The following example shows CFTRY and CFCATCH, using a sample data source called company and a sample included file, includeme.cfm .
If the data access driver raises an exception during the CFQUERY statement's execution, the application page flow continues to the CFCATCH TYPE="Database" exception handler. It then resumes with the next statement after the CFTRY block, once the CFCATCH TYPE="Database" handler completes.
Similarly, the CFCATCH TYPE="MissingInclude" block handles exceptions raised by the CFINCLUDE tag. Any unknown, but possibly recoverable, exceptions are handled by the CFCATCH TYPE="Any" block.
<!--- Wrap code you want to check in a CFTRY block --->
<CFTRY>
<CFQUERY NAME="test" DATASOURCE="company">
SELECT DepartmentID, FirstName, LastName
FROM employees
WHERE employeeID=#EmpID#
</CFQUERY>
<HTML>
<HEAD>
<TITLE>Test CFTRY/CFCATCH</TITLE>
</HEAD>
<BODY>
<HR>
<CFINCLUDE TEMPLATE="includeme.cfm">
<CFOUTPUT QUERY="test">
<P>Department: #DepartmentID#
<P>Last Name: #LastName#
<P>First Name: #FirstName#
</CFOUTPUT>
<HR>
<!--- Use CFCATCH to test for missing included files.
Print Message and Detail error messages. --->
<CFCATCH TYPE="MissingInclude">
<H1>Missing Include File</H1>
<CFOUTPUT>
<UL>
<LI><b>Message:</b> #CFCATCH.Message#
<LI><b>Detail:</b> #CFCATCH.Detail#
<LI><b>File name:</b> #CFCATCH.MissingFilename#
</UL>
</CFOUTPUT>
</CFCATCH>
<!--- Use CFCATCH to test for database errors.
Print error messages. --->
<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>
</CFCATCH>
<!--- Use CFCATCH with TYPE="Any"
to find unexpected exceptions. --->
<CFCATCH TYPE="Any">
<H1>Other Error: #CFCATCH.Type#</H1>
<CFOUTPUT>
<UL>
<LI><b>Message:</b> #CFCATCH.message#
<LI><b>Detail:</b> #CFCATCH.Detail#
</UL>
</CFOUTPUT>
</CFCATCH>
</CFTRY>
</BODY>
</HTML>
|