User Guide
204 Chapter 11 Preventing and Handling Errors
Handling Exceptions in ColdFusion
Ordinarily, when ColdFusion encounters an error, it stops processing and displays
an error message or error page (as specified by the
cferror tag). However, you can
use ColdFusion’s exception handling tags to catch and process exceptions in
ColdFusion pages. Exceptions include any event that disrupts the normal flow of
instructions in a ColdFusion page, such as failed database operations, missing
include files, or developer-specified events.
In order for your code to handle an exception, the tags in question must appear
within a
cftry block. It is a good idea to enclose an entire application page in a cftry
block. You then follow the
cftry block with cfcatch blocks, which respond to
potential errors. When an exception occurs within the
cftry block, processing is
thrown to the
cfcatch block for that type of exception.
Note
For cases when the error handler is not able to successfully handle the thrown error,
use the cfrethrow tag within a cfcatch block.
Here is an outline for using cftry and cfcatch to handle errors:
<cftry>
... Put your page’s 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>
To catch errors in a single problematic SQL statement, for example, you might
narrow the focus by surrounding the ColdFusion tag that contains the SQL
statement with a cftry block and following it with a cfcatch type="Database" tag
that displays the information in the
cfcatch.SQLState variable.
Note
Do not attempt to enclose an entire application in a
cftry block by putting the cftry
tag in Application.cfm because you cannot be sure that there always will be a
matching cftry end tag.
For information on the cftry, cfcatch, cfrethrow, and cfthrow tags, see the CFML
Reference.