Since version 4, CoidFuslon has provided a more structured way of handling errors within ColdFusion templates. This new type of error handling enables you to account for specific types of errors within a specific template, without necessarily aborting the processing of the entire template. Remember that with Va1i dati on and Request types of the CFERROR tag tile processing of the template is aborted and the user is redirected to a custom error page. CFTRY and CFCATCH enable errors to be handled directly within the template throwing the exception. They can also be used with ColdFusion code to provider pages that can be more easily customized. To use this new error-handling code you must learn about the tags.
Specifically, you must enclose the code you wish to be checked for errors within the CFTRY tags. You can enclose an entire template or just a particular piece of code. You must also include one or more CFCATCH tags inside the CFTRY tag pair; they should be placed immediately before the end CFTRY tag. The general format should look like the following:
The following code shows an example of using CFTRY and CFCATCH to catch a database error. How this template might look in the browser if an error was caught.
In the preceding code, the CFCATCHtag will catch any database errors that occur within the CFTRYtags, notify the user that an error has occurred, provide specific details’ through the use of built-in variables (shown in Table 18.5) and then continue with the processing of the’ template.’ If any other type of error occurs, a standard ColdFusion error message will be displayed and the processing of the template will be aborted. In the next section you will look at other types of errors that can be caught by using’CFTRY
Types of Errors
Using CFTRYand CFCATCHy,ou can handle up to 10 types of errors. The error type is specified in the TYPEattribute of the CFCATCHtag. The syntax is written as follows: <FCATCH TYPE-‘type_of_error’) These error types are described in the following list:
A location Default. Catches errors raised with the CFTHROWtag,