Exception Handling in <cfscript>
CFML Error handling in general is covered in Chapter 51, “Error Handling,” online. If you’re already familiar with implementing structured exception handling in your CFML code, then you’re also familiar with using <cftry>-<cfcatch> constructs, and the various types of errors that can be handled by specifying type attributes in your <cfctach> blocks.
Fortunately, ColdFusion scripting provides an equivalent with try-catch. Listing 44.13 demonstrates a comparison of tag-based and script-based try-catch handling. To run Listing 44.13, note that there it tries to open a file named test.txt which has been provided in the sample source code. Try changing filename to test2.txt (which does not exist) and notice the types of exceptions that are thrown, caught, and handled.
Listing 44.13. ExceptionHandling.cfm—Handling Exceptions in ColdFusion Script
<!--- Author: Charlie Arehart -- carehart.org ---> <!--- Exception handling ---> <cfset filename="test.txt"> <!--- Tag-based ---> <cftry> <cfset result = FileOpen(expandpath(filename))> <p>It worked!</p> <cfcatch type="Expression"> <p>An Expression exception was thrown.</p> <cfoutput>#cfcatch.message#</cfoutput> </cfcatch> <cfcatch type="Security"> <p>A Security exception was thrown.</p> <cfoutput>#cfcatch.message#</cfoutput> </cfcatch> </cftry> <cfscript> // Script-based try { result = FileOpen(expandpath(filename)); WriteOutput("<p>It worked!</p>"); } catch(Expression exceptionVariable) { WriteOutput("<p>An Expression exception was thrown.</p>"); WriteOutput("<p>#exceptionVariable.message#</p>"); } catch(Security exceptionVariable) { WriteOutput("<p>A Security exception was thrown.</p>"); WriteOutput("<p>#exceptionVariable.message#</p>"); } </cfscript>
There are a couple of differences between try-catch in <cfscript> and the tag-based equivalents. First, note that the tag-based <cfcatch> is placed within a <cftry>, where a script-based catch is placed after a try. Also, note that with <cfcatch>, there’s an available, pre-defined catch scope holding details of the error, where with the catch statement, you instead name a variable to hold those details.
Perhaps a more significant difference is that you can’t execute tags that you might normally for error handling, such as <cfmail>, <cflog>, <cfquery>, etc. At least in tag-based try-catch processing, you could <cfrethrow> a caught exception or <cfthrow> a custom exception of your own design, to be handled by a higher-level error handler. Unfortunately, there is no way to rethrow an exception once it is caught in the scripting version of a try-catch construct; nor can you throw custom exceptions in ColdFusion scripting. You could, however, call a <cffunction>-based user-defined function, which then could perform normal tag-based processing to send email, create a log, etc.
Script-based processing in CFML is just another option in your CFML toolbelt. There may be times when its simpler syntax (fewer tags) will appeal to you as you write code. More often, it will appeal to those coming to CFML from other languages. As so often with ColdFusion, the choice is yours.