Whatever message this page gives is out now! Go check it out!
| Tag | Description |
| cftry | If any exceptions occur while processing the tag body, look for a cfcatch tag that handles the exception, and execute the code in the cfcatch tag body. |
| cfcatch | Execute code in the body of this tag if the exception caused by the code in the cftry tag body matches the exception type specified in this tag's attributes.Used in cftry tag bodies only. |
| cfthrow | Generate a user-specified exception. |
| cfrethrow | Exit the current cfcatch block and generates a new exception of the same type. Used only in cfcatch tag bodies. |
| <cftry> Put your application code here ... <cfcatch type="exception type1"> Add exception processing code here ... </cfcatch> <cfcatch type="exception type2"> Add exception processing code here ... </cfcatch> ... <cfcatch type="Any"> Add exception processing code appropriate for all other exceptions here ... </cfcatch> </cftry> |
| Property variable | Description |
| cfcatch.Detail | A detailed message from the CFML compiler. This message, which can contain HTML formatting, can help to determine which tag threw the exception. The cfcatch.Detail value is available in the CFScript catch statement as the exceptionVariable parameter. |
| cfcatch.ErrorCode | The cfthrow tag can supply a value for this code through the errorCode attribute. For Type="Database", cfcatch.ErrorCode has the same value as cfcatch.SQLState. Otherwise, the value of cfcatch.ErrorCode is the empty string. |
| cfcatch.ExtendedInfo | Custom error message information. This is returned only to cfcatch tags for which the type attribute is Application or a custom type.Otherwise, the value of cfcatch.ExtendedInfo is the empty string. |
| cfcatch.Message | The exception's default diagnostic message, if one was provided. If no diagnostic message is available, this is an empty string.The cfcatch.Message value is included in the value of the CFScript catch statement exceptionVariable parameter. |
| cfcatch.RootCause | The Java servlet exception reported by the JVM as the cause of the "root cause" of the exception. |
| cfcatch.TagContext | An array of structures structure containing information for each tag in the tag stack The tag stack consists of each tag that is currently open. |
| cfcatch.Type | The exception's type, returned as a string. |
| Note: If you use the cfdump tag to display the cfcatch variable, the display does not include variables that do not have values. |
| Entry | Description |
| Column | Obsolete (retained for backwards compatibility). Always 0. |
| ID | The tag in which the exception occurred. Exceptions in CFScript are indicated by two question marks (??). All custom tags, including those called directly, are identified as cfmodule. |
| Line | The line on the page in which the tag is located. |
| Raw_Trace | The raw Java stack trace for the error. |
| Template | The pathname of the application page that contains the tag. |
| Type | The type of page; it is always a ColdFusion page. |
| Property variable | Description |
| cfcatch.NativeErrorCode | The native error code associated with this exception. Database drivers typically provide error codes to assist in the diagnosis of failing database operations. The values assumed by cfcatch.NativeErrorCode are driver-dependent.If no error code is provided, the value of cfcatch.nativeErrorCode is -1. The value is 0 for queries of queries. |
| cfcatch.SQLState | The SQLState code associated with this exception. Database drivers typically provide error codes to assist in the diagnosis of failing database operations. SQLState codes are more consistent across database systems than native error codes. If the driver does not provide an SQLState value, the value of cfcatch.SQLState is -1. |
| cfcatch.Sql | The SQL statement sent to the data source. |
| cfcatch.queryError | The error message as reported by the database driver. |
| cfcatch.where | If the query uses the cfqueryparam tag, query parameter name-value pairs. |
| Property variable | Description |
| cfcatch.ErrNumber | An internal expression error number, valid only when type="Expression". |
| Property variable | Description |
| cfcatch.lockName | The name of the affected lock. This is set to "anonymous" if the lock name is unknown. |
| cfcatch.lockOperation | The operation that failed. This is set to "unknown" if the failed operation is unknown. |
| Property variable | Description |
| cfcatch.missingFileName | The name of the missing file. |
| <!--- Wrap code you want to check in a cftry block ---> <cfset EmpID=3> <cfparam name="errorCaught" default=""> <cftry> <cfquery name="test" datasource="cfdocexamples"> SELECT Dept_ID, FirstName, LastName FROM Employee WHERE Emp_ID=#EmpID# </cfquery> <html> <head> <title>Test cftry/cfcatch</title> </head> <body> <cfinclude template="includeme.cfm"> <cfoutput query="test"> <p>Department: #Dept_ID#<br> Last Name: #LastName#<br> First Name: #FirstName#</p> </cfoutput> <!--- Use cfcatch to test for missing included files. ---> <!--- Print Message and Detail error messages. ---> <!--- Block executes only if a MissingInclude exception is thrown. ---> <cfcatch type="MissingInclude"> <h1>Missing Include File</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# <li><b>Filename:</b> #cfcatch.MissingFileName# </ul> </cfoutput> <cfset errorCaught = "MissingInclude"> </cfcatch> <!--- Use cfcatch to test for database errors.---> <!--- Print error messages. ---> <!--- Block executes only if a Database exception is thrown. ---> <cfcatch type="Database"> <h1>Database Error</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Native error code:</b> #cfcatch.NativeErrorCode# <li><b>SQLState:</b> #cfcatch.SQLState# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "Database"> </cfcatch> <!--- Use cfcatch with type="Any" ---> <!--- to find unexpected exceptions. ---> <cfcatch type="Any"> <cfoutput> <hr> <h1>Other Error: #cfcatch.Type#</h1> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "General Exception"> </cfcatch> </body> </html> </cftry> |
| Code | Description | |
| <cfset EmpID=3> <cfparam name="errorCaught" default=""> | <cfset EmpID=3> <cfparam name="errorCaught" default=""> | Initializes the employee ID to a valid value. An application would get the value from a form or other source.Sets the default errorCaught variable value to the empty string (to indicate no error was caught).There is no need to put these lines in a cftry block. |
| <cfset EmpID=3> <cfparam name="errorCaught" default=""> | ||
| <cftry> <cfquery name="test" datasource="cfdocexamples"> SELECT Dept_ID, FirstName, LastName FROM Employee WHERE Emp_ID=#EmpID# </cfquery> | <cftry> <cfquery name="test" datasource="cfdocexamples"> SELECT Dept_ID, FirstName, LastName FROM Employee WHERE Emp_ID=#EmpID# </cfquery> | Starts the cftry block. Exceptions from here to the end of the block can be caught by cfcatch tags.Queries the cfdocexamples database to get the data for the employee identified by the EmpID variable. |
| <cftry> <cfquery name="test" datasource="cfdocexamples"> SELECT Dept_ID, FirstName, LastName FROM Employee WHERE Emp_ID=#EmpID# </cfquery> | ||
| <html> <head> <title>Test cftry/cfcatch</title> </head> <body> <cfinclude template="includeme.cfm"> <cfoutput query="test"> <p>Department: #Dept_ID#<br> Last Name: #LastName#<br> First Name: #FirstName#</p> </cfoutput> | <html> <head> <title>Test cftry/cfcatch</title> </head> <body> <cfinclude template="includeme.cfm"> <cfoutput query="test"> <p>Department: #Dept_ID#<br> Last Name: #LastName#<br> First Name: #FirstName#</p> </cfoutput> | Begins the HTML page. This section contains all the code that displays information if no errors occur.Includes the includeme.cfm page.Displays the user information record from the test query. |
| <html> <head> <title>Test cftry/cfcatch</title> </head> <body> <cfinclude template="includeme.cfm"> <cfoutput query="test"> <p>Department: #Dept_ID#<br> Last Name: #LastName#<br> First Name: #FirstName#</p> </cfoutput> | ||
| <cfcatch type="MissingInclude"> <h1>Missing Include File</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# <li><b>Filename:</b> #cfcatch.MissingFileName# </ul> </cfoutput> <cfset errorCaught = "MissingInclude"> </cfcatch> | <cfcatch type="MissingInclude"> <h1>Missing Include File</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# <li><b>Filename:</b> #cfcatch.MissingFileName# </ul> </cfoutput> <cfset errorCaught = "MissingInclude"> </cfcatch> | Handles exceptions thrown when a page specified by the cfinclude tag cannot be found. Displays cfcatch variables, including the ColdFusion basic error message, detail message, and the name of the file that could not be found.Sets the errorCaught variable to indicate the error type. |
| <cfcatch type="MissingInclude"> <h1>Missing Include File</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# <li><b>Filename:</b> #cfcatch.MissingFileName# </ul> </cfoutput> <cfset errorCaught = "MissingInclude"> </cfcatch> | ||
| <cfcatch type="Database"> <h1>Database Error</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Native error code:</b> #cfcatch.NativeErrorCode# <li><b>SQLState:</b> #cfcatch.SQLState# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "Database"> </cfcatch> | <cfcatch type="Database"> <h1>Database Error</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Native error code:</b> #cfcatch.NativeErrorCode# <li><b>SQLState:</b> #cfcatch.SQLState# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "Database"> </cfcatch> | Handles exceptions thrown when accessing a database. Displays cfcatch variables, including the ColdFusion basic error message, the error code and SQL state reported by the databases system, and the detailed error message.Sets the errorCaught variable to indicate the error type. |
| <cfcatch type="Database"> <h1>Database Error</h1> <cfoutput> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Native error code:</b> #cfcatch.NativeErrorCode# <li><b>SQLState:</b> #cfcatch.SQLState# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "Database"> </cfcatch> | ||
| <cfcatch type="Any"> <cfoutput> <hr> <h1>Other Error: #cfcatch.Type#</h1> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "General Exception"> </cfcatch> | <cfcatch type="Any"> <cfoutput> <hr> <h1>Other Error: #cfcatch.Type#</h1> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "General Exception"> </cfcatch> | Handles any other exceptions generated in the cftry block.Since the error can occur after information has displayed (in this case, the contents of the include file), draws a line before writing the message text.Displays the ColdFusion basic and detailed error message.Sets the errorCaught variable to indicate the error type. |
| <cfcatch type="Any"> <cfoutput> <hr> <h1>Other Error: #cfcatch.Type#</h1> <ul> <li><b>Message:</b> #cfcatch.Message# <li><b>Detail:</b> #cfcatch.Detail# </ul> </cfoutput> <cfset errorCaught = "General Exception"> </cfcatch> | ||
| </body> </html> </cftry> | </body> </html> </cftry> | Ends the HTML page, then the cftry block. |
| </body> </html> </cftry> |
| Attribute | Meaning |
| type | The type of error. It can be a custom type that has meaning only to your application, such as InvalidProductCode. You can also specify Application, the default type. You cannot use any of the predefined ColdFusion error types, such as Database or MissingTemplate. |
| message | A brief text message indicating the error. |
| detail | A more detailed text message describing the error. |
| errorCode | An error code that is meaningful to the application. This field is useful if the application uses numeric error codes. |
| extendedInfo | Any additional information of use to the application. |
| <cfthrow type="com.myCompany.myApp.Invalid_field.codeValue" errorcode="Dodge14B"> |
| <cfthrow type="MyApp.BusinessRuleException.InvalidAccount"> |
| <cfcatch type="MyApp.BusinessRuleException.InvalidAccount"> <cfcatch type="MyApp.BusinessRuleException"> <cfcatch type="MyApp"> |
| <cfthrow type="MyApp.BusinessRuleException.InvalidVendorCode |
| <cftry> <cftry> Code that might throw a database error <cfcatch Type="Database"> <cfif Error is of type I can Handle> Handle it <cfelse> <cfrethrow> </cfif </cfcatch> </cftry> <cfcatch Type="Any"> General Error Handling code </cfcatch> </cftry> |
| <cftry> <cf_getEmps EmpName="Jones"> <cfcatch type="myApp.getUser.noEmpName"> <h2>Oops</h2> <cfoutput>#cfcatch.Message#</cfoutput><br> </cfcatch> </cftry> <cfif isdefined("getEmpsResult")> <cfdump var="#getEmpsResult#"> </cfif> |
| Code | Description | |
| <cftry> <cf_getEmps EmpName="Jones"> | <cftry> <cf_getEmps EmpName="Jones"> | In a cftry block, calls the cf_getEmps custom tag (getEmps.cfm). |
| <cftry> <cf_getEmps EmpName="Jones"> | ||
| <cfcatch type="myApp.getUser.noEmpName"> <h2>Oops</h2> <cfoutput>#cfcatch.Message#</cfoutput><br> </cfcatch> | <cfcatch type="myApp.getUser.noEmpName"> <h2>Oops</h2> <cfoutput>#cfcatch.Message#</cfoutput><br> </cfcatch> | If the tag throws an exception indicating that it did not receive a valid attribute, catches the exception and displays a message, including the message variable set by the cfthrow tag in the custom tag. |
| <cfcatch type="myApp.getUser.noEmpName"> <h2>Oops</h2> <cfoutput>#cfcatch.Message#</cfoutput><br> </cfcatch> | ||
| <cfif isdefined("getEmpsResult")> <cfdump var="#getEmpsResult#"> </cfif> | <cfif isdefined("getEmpsResult")> <cfdump var="#getEmpsResult#"> </cfif> | If the tag returns a result, uses the cfdump tag to display it. (A production application would not use the cfdump tag.) |
| <cfif isdefined("getEmpsResult")> <cfdump var="#getEmpsResult#"> </cfif> |
| <!--- If the tag didn't pass an attribute, throw an error to be handled by the calling page ---> <cfif NOT IsDefined("attributes.EmpName")> <cfthrow Type="myApp.getUser.noEmpName" message = "Last Name was not supplied to the cf_getEmps tag."> <cfexit method = "exittag"> <!--- Have a name to look up ---> <cfelse> <!--- Outermost Try Block ---> <cftry> <!--- Inner Try Block ---> <cftry> <!--- Try to query the main database and set a caller variable to the result ---> <cfquery Name = "getUser" DataSource="cfdocexamples"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> <!--- If the query failed with a database error, check the error type to see if the database was found ---> <cfcatch type= "Database"> <cfif (cfcatch.SQLState IS "S100") OR (cfcatch.SQLState IS "IM002")> <!--- If the database wasn't found, try the backup database ---> <!--- Use a third-level Try block ---> <cftry> <cfquery Name = "getUser" DataSource="cfdocexamplesBackup"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> <!--- If still get a database error, just return to the calling page without setting the caller variable. There is no cfcatch body. This might not be appropriate in some cases. The Calling page ends up handling this case as if a match was not found ---> <cfcatch type = "Database" /> <!--- Still in innermost try block. Rethrow any other errors to the next try block level ---> <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> <!--- Now in second level try block. Throw all other types of Database exceptions to the next try block level ---> <cfelse> <cfrethrow> </cfif> </cfcatch> <!--- Throw all other exceptions to the next try block level ---> <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> <!--- Now in Outermost try block. Handle all unhandled exceptions, including rethrown exceptions, by displaying a message and exiting to the calling page.---> <cfcatch Type = "Any"> <h2>Sorry</h2> <p>An unexpected error happened in processing your user inquiry. Please report the following to technical support:</p> <cfoutput> Type: #cfcatch.Type# Message: #cfcatch.Message# </cfoutput> <cfexit method = "exittag"> </cfcatch> </cftry> </cfif> |
| Code | Description | |
| <cfif NOT IsDefined("attributes.EmpName")> <cfthrow Type="myApp.getUser.noEmpName" message = "Last Name was not supplied to the cf_getEmps tag."> <cfexit method = "exittag"> | <cfif NOT IsDefined("attributes.EmpName")> <cfthrow Type="myApp.getUser.noEmpName" message = "Last Name was not supplied to the cf_getEmps tag."> <cfexit method = "exittag"> | Makes sure the calling page specified an EmpName attribute. If not, throws a custom error that indicates the problem and exits the tag. The calling page handles the thrown error. |
| <cfif NOT IsDefined("attributes.EmpName")> <cfthrow Type="myApp.getUser.noEmpName" message = "Last Name was not supplied to the cf_getEmps tag."> <cfexit method = "exittag"> | ||
| <cfelse> <cftry> | <cfelse> <cftry> | If the tag has an EmpName attribute, does the remaining work inside an outermost try block. The cfcatch block at its end handles any otherwise-uncaught exceptions. |
| <cfelse> <cftry> | ||
| <cftry> <!--- Try to query the main database and set a caller variable to the result ---> <cfquery Name = "getUser" DataSource="cfdocexamples"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> | <cftry> <!--- Try to query the main database and set a caller variable to the result ---> <cfquery Name = "getUser" DataSource="cfdocexamples"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> | Starts a second nested try block. This block catches exceptions in the database query. If there are no exceptions, sets the calling page's getEmpsResult variable with the query results. |
| <cftry> <!--- Try to query the main database and set a caller variable to the result ---> <cfquery Name = "getUser" DataSource="cfdocexamples"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> | ||
| <cfcatch type= "Database"> <cfif (cfcatch.SQLState IS "S100") OR (cfcatch.SQLState IS "IM002")> <cftry> <cfquery Name = "getUser" DataSource="cfdocexamplesBackup"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> | <cfcatch type= "Database"> <cfif (cfcatch.SQLState IS "S100") OR (cfcatch.SQLState IS "IM002")> <cftry> <cfquery Name = "getUser" DataSource="cfdocexamplesBackup"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> | If the query threw a Database error, checks to see if the error was caused by an inability to access the database (indicated by an SQLState variable value of S100 or IM002). If the database was not found, starts a third nested try block and tries accessing the backup database. This try block catches exceptions in this second database access.If the database inquiry succeeds, sets the calling page's getEmpsResult variable with the query results. |
| <cfcatch type= "Database"> <cfif (cfcatch.SQLState IS "S100") OR (cfcatch.SQLState IS "IM002")> <cftry> <cfquery Name = "getUser" DataSource="cfdocexamplesBackup"> SELECT * FROM Employee WHERE LastName = '#attributes.EmpName#' </cfquery> <cfset caller.getEmpsResult = getuser> | ||
| <cfcatch type = "Database" /> | <cfcatch type = "Database" /> | If the second database query failed with a database error, gives up silently. Because the Database type cfcatch tag does not have a body, the tag exits. The calling page does not get a getEmpsResult variable. It cannot tell whether the database had no match or an unrecoverable database error occurred, but it does know that no match was found. |
| <cfcatch type = "Database" /> | ||
| <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> | <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> | If the second database query failed for any other reason, throws the error up to the next try block.Ends the innermost try block |
| <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> | ||
| <cfelse> <cfrethrow> </cfif> </cfcatch> | <cfelse> <cfrethrow> </cfif> </cfcatch> | In the second try block, handles the case in which the first database query failed for a reason other than a failure to find the database. Rethrows the error up to the next level, the outermost try block. |
| <cfelse> <cfrethrow> </cfif> </cfcatch> | ||
| <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> | <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> | In the second try block, catches any errors other exceptions and rethrows them up to the outermost try block. Ends the second try block. |
| <cfcatch type = "Any"> <cfrethrow> </cfcatch> </cftry> | ||
| <cfcatch Type = "Any"> <h2>Sorry</h2> <p>An unexpected error happened in processing your user inquiry. Please report the following to technical support:</p> <cfoutput> Type: #cfcatch.Type# Message: #cfcatch.Message# </cfoutput> <cfexit method = "exittag"> </cfcatch> </cftry> </cfif> | <cfcatch Type = "Any"> <h2>Sorry</h2> <p>An unexpected error happened in processing your user inquiry. Please report the following to technical support:</p> <cfoutput> Type: #cfcatch.Type# Message: #cfcatch.Message# </cfoutput> <cfexit method = "exittag"> </cfcatch> </cftry> </cfif> | In the outermost try block, handles any exceptions by displaying an error message that includes the exception type and the exception's error message. Because there was no code to try that is not also in a nested try block, this cfcatch tag handles only errors that are rethrown from the nested blocks.Exits the custom tag and returns to the calling page.Ends the catch block, try block, and initial cfif block. |
| <cfcatch Type = "Any"> <h2>Sorry</h2> <p>An unexpected error happened in processing your user inquiry. Please report the following to technical support:</p> <cfoutput> Type: #cfcatch.Type# Message: #cfcatch.Message# </cfoutput> <cfexit method = "exittag"> </cfcatch> </cftry> </cfif> |
<cftry>
<cfthrow type="Database" message="Test exception">
<cfcatch type="Database">
<cfoutput>#cfcatch.type#: #cfcatch.message#</cfoutput>
</cfcatch>
</cftry>