Exception Handling in cfscript


Exception Handling in <cfscript>

There are three levels of exception handling in ColdFusion:

  • Exceptions that you reasonably expect might happen within your code, such as a user-submitted zero value that is used as a divisor in a calculation. These exceptions are handled using TRy-catch constructs. Exceptions handled by try-catch constructs typically suggest alternate workflows, such as allowing the user to enter a different value.

  • Exceptions that do not have any alternate workflow, such as a failure to connect to the database, are handled by error-handling templates that are installed using <cferror> or handled by the onError method in Application.cfc.

  • Exceptions that are handled neither by try-catch constructs or installed error handlers fall through to the sitewide error handler, if one is installed (which it should be on a production system).

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 tag-based version in its entirety, and alternately comment out the first assignment to result (each assignment throws a different exception). Run the listing once with the assignment uncommented, then again with it commented out, and notice the types of exceptions that are thrown, caught, and handled.

To enable ColdFusion security and disable the FileExists() function:

1.

Launch ColdFusion Administrator and login.

2.

Expand the Security section of the left-side navigation bar.

3.

Click the Sandbox Security link.

4.

Select the Enable ColdFusion Security checkbox, then click the Submit Changes button.

5.

Click the Browse Server button, navigate to your Chapter12 directory, select it, and click the Apply button.

6.

Leave the select menu on the New Sandbox, or pick one to copy from option, then click the Add button.

7.

In the Defined Directory Permissions list, click the entry for your Chapter12 directory.

8.

Click the CF Functions tab, select the FileExists() function, and click the right arrow button to move that function to the Disabled Functions list.

9.

Click the Finish button, then logout from ColdFusion Administrator.

10.

Cycle the ColdFusion Server service so your settings can take effect.

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.cfmHandling 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 excuse for failing to implement exception handling. Make sure you always implement exception handling in ColdFusion scripts when handling such an exception will enable you to provide your user with an alternate workflow (try again, try something else, etc.). If no alternate workflow is possible, let any exception fall through to whatever error-handling template or method is installed by your ColdFusion application framework.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net