User Guide

Table Of Contents
324 Chapter 14: Handling Errors
Here is an outline for using cftry and cfcatch to handle errors:
<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>
Try/catch code rules and recommendations
Follow these rules and recommendations when you use
cftry and cfcatch tags:
The cfcatch tags must follow all other code in a cftry tag body.
You can nest cftry blocks. For example, the following structure is valid:
<cftry>
code that may cause an exception
<cfcatch ...>
<cftry>
First level of exception handling code
<cfcatch ...>
Second level of exception handling code
</cfcatch
</cftry>
</cfcatch>
</cftry>
If an exception occurs in the first level of exception-handling code, the inner cfcatch block
can catch and handle it. (An exception in a
cfcatch block cannot be handled by cfcatch
blocks at the same level as that block.)
ColdFusion always responds to the latest exception that gets raised. For example, if code in a
cftry block causes an exception that gets handled by a cfcatch block, and the cfcatch block
causes an exception that has no handler, ColdFusion will display the default error message for
the exception in the
cfcatch block, and you will not be notified of the original exception.
If an exception occurs when the current tag is nested inside other tags, the CFML processor
checks the entire stack of open tags until it finds a suitable
cftry/cfcatch combination or
reaches the end of the stack.
Use cftry with cfcatch to handle exceptions based on their point of origin within an
application page, or based on diagnostic information.
The entire cftry tag, including all its cfcatch tags, must be on a single ColdFusion page. You
cannot put the
<cftry> start tag on one page and have the </cftry> end tag on another page.