Error Handling Within the Web-Application Framework

While cftry and cfcatch provide a granular means of handling errors and exceptions within your applications, they can be cumbersome to code. Trying to identify all the potential trouble spots in your applications where cftry/cfcatch code should be placed only compounds the problem. Fortunately, ColdFusion provides a way to handle errors at a more general level. By including ColdFusion’s cferror tag within your application’s Application.cfm template, you can implement application-specific “catch all” error handlers.

Although the cferror tag can be used in other templates besides Application.cfm, it makes the most sense to use it in this template. The cferror tag is generally placed directly below the cfapplication tag:

<cfapplication name="MyApplication">
   
<!--- Implement error handling --->
<cferror type="Exception" template="exception_handler.cfm"
         exception="Any" mailto="webmaster@example.com">

The cferror tag can implement one of four types of error handling, depending on the value specified in the type attribute. The template attribute is also required; it specifies the relative path to a custom error template to execute in the event that an error or exception occurs. Depending on the value specified in the type attribute, different options are available to the error-handling template. Here are the values for type and the corresponding options that are available in the error template:

Exception

Handles a specific exception ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.