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 e-mail address so the company staff can enter the data 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.