Exception Handling in <cfscript>There are three levels of exception handling in ColdFusion:
These three levels provide a structured exception-handling framework you can engage to correctly handle any exception thrown by your application. If you're familiar with implementing structured exception handling in your 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 a low-grade equivalent with try-catch . Unfortunately, though, you can neither rethrow a caught exception nor throw a custom exception of your own design.
To run Listing 12.13, you'll need to do a little configuration work. First, enable ColdFusion Security in ColdFusion Administrator, disable the
FileExists()
function in your security sandbox, and then restart ColdFusion Server. You'll also want to comment out the
To enable ColdFusion security and disable the FileExists() function:
When you're done experimenting with Listing 12.13, retrace your steps to re-enable the FileExists() function.
NOTE The instructions above are for ColdFusion Enterprise, which enables you to create multiple security sandboxes. If you are running ColdFusion Standard then your settings will apply to all ColdFusion pages running on the server. Listing 12.13. ExceptionHandling.cfm Handling Exceptions in ColdFusion Script
<!--- Author: Adam Phillip Churvis -- ProductivityEnhancement.com --->
<!--- Exception handling --->
<!--- Tag-based --->
<cftry>
<cfset result = 1/0>
<cfset result = FileExists("c:\SomeFile.txt")>
<p>It worked!</p>
<cfcatch type="Expression">
<p>An Expression exception was thrown.</p>
</cfcatch>
<cfcatch type="Security">
<p>A Security exception was thrown.</p>
</cfcatch>
</cftry>
<cfscript>
// Script-based
try {
result = 1/0;
result = FileExists("c:\SomeFile.txt");
WriteOutput("<p>It worked!</p>");
}
catch(Expression exceptionVariable) {
WriteOutput("<p>An Expression exception was thrown.</p>");
}
catch(Security exceptionVariable) {
WriteOutput("<p>A Security exception was thrown.</p>");
}
</cfscript>
Remember that 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. So your exception-handling capabilities are limitedbut that is no
|