User Guide

Table Of Contents
Handling runtime exceptions with ColdFusion tags 323
This ability to handle exceptions directly in your application pages enables your application to do
the following:
Respond appropriately to specific errors within the context of the current application page
Recover from errors whenever possible.
Exception-handling tags
ColdFusion provides the exception-handling tags listed in the following table:
Using cftry and cfcatch tags
The
cftry tag lets you go beyond reporting error data to the user:
You can include code that recovers from errors so your application can continue processing
without alerting the user.
You can create customized error messages that apply to the specific code that causes the error.
For example, you can use
cftry to catch errors in code that enters data from a user registration
form to a database. The
cfcatch code could do the following:
1.
Retry the query, so the operation succeeds if the resource was only temporarily unavailable.
2.
If the retries fail:
Display a custom message to the user
Post the data to an email address so the data could be entered by company staff after the
problem has been solved.
Code that accesses external resources such as databases, files, or LDAP servers where resource
availability is not guaranteed is a good candidate for using try/catch blocks.
Try/catch code structure
In order for your code to directly 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.
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.