You can use the CFTHROW tag to create your own exceptions. The exceptions created by a CFTHROW tag can be caught by CFCATCH tags with the TYPE attributes of Application, Any, or your own custom type. The general format should look like this:
The following list describes each attribute that is used with CFTHROW(all attributes are optional):
TYPE Application, Any, ora custom name.
MESSAGE A message you want to display to &h~user.
DETAlL ColdFusion provides a detailed description of the error. You can add to this description when using the DETAIL attribute.
ERRORCODE A specific error code you want to display to the user.
EXTENDEDINFO Any additional information you wish to specify
If you specify a type when using the CFTOW tag, then the corresponding CFCATCHtag must have the same type name. This.nadre can be App1; cat; on, Any, or a name defined by you. It should not be any of the other default types (such as Database or Lock).
The CFTHROW variables avciilable inSide the CFCATCH tags are:
.• CFCATCH.DETAIL .
Refer to present earlier, for more details about these variables.
The results might look like those. The following code shows an example of how to use CFTHROW with the CFTRY and CFCATCH tags:
Catching Exceptions with CFERROR
As mentioned earlier in this chapter, when you reviewed the CFERROR tag from Cold-Fusion 4.5, the CFERROR tag supports the Except; on type that allows you to specify an exception-handling template in the event of exceptions. This allows you to create an exception-handling template that you can apply to all templates in an application instead of writing exception-handling code. for each template. In order to understand the significance of this, consider the last example of the CFTHROW tag:
Here we have used the CFTRYand CFCATCHtags to throw, catch, and handle a custom exception of the type _Error. wanted to catch this type of exception for all templates in your application, you would use the CFERRORtag in your Application. efm template ~tead of the CFTRYand CFCATCHtags in each template.
First, you would want to add the following CFERRORtag to your Application. efm file: <CFERROR TYPE-‘Excepti, on’ EXCEPTION-‘ My_Error, TEMPLATE-‘My error.cfm’> , You can specify CFERRORto catch any exception that could be specified in a CFCATCH tag, as outlined earlier in this chapter.
Notice that we have completely eliminated the CFTRYand CFCATCHcode. The CFERROR .tag in the Application .cfm file will catch the exception generated by the CFTHROW tag. Then the execution of the template will be stopped and the myerror. cfm exception handling template. specified in the CFERRORtag will be executed.
The major difference is that you are using the Error structure (as you did in Request and Val i dati on error templates) to access information about’ the error. All the CFCATCH variables outlined are available in the. Error structure, as are all of those outlined.
Where Do We Go from Here?
In this chapter, you learned how to control errors in your ColdFusion templates and throughout your applications. You learned about general application error control using ERRORand you learned how to handle more structured errors using CFTRY,CFCATCH, and CFTHROW. In the next chapter, you will learn how to manage files on your server. Remember the information that you have learned in this chapter and try to apply it to the ColdFusion code you will learn in the following chapters.