Handling Exceptions


No matter how great a coder you are, you cannot foresee all the things that might happen when your code or application is actually used in production. For this reason, almost every programming language or scripting language has some method of exception handling to enable developers to create robust applications that gracefully handle unforeseen events or that provide useful and often critical debugging information so that you can find the source of the actual problem and resolve it.

At this point, you are familiar with CFML exception handling, but CFScript also has its own exception handling with its analogues to the CFTRY and CFATCH tags. CFScript provides you the try, catch, and finally script statements, and when you develop your CFScript blocks (especially when creating UDFs), you should use them liberally!

CFScript Exception Syntax

Using exception-handling code in CFScript is very similar to using it in standard CFML. For example, let's look at the generic form of a try/catch statement in CFScript:

 try{       Code you wish to have handled if there is a exception}       catch(exceptionType exceptionVariable )       {The code you will use to deal with the exception that is characterized by       exceptionType}       catch(exceptionType2 exceptionVariable2 )       {The code you will use to deal with the exception that is characterized by       exceptionType2}       etc...       {The code you want to be processed in all cases, regardless of if there was an       exception} 

Now if you remember from CFML exception handling, a CFML CFTRY block must have CFCATCH and CFINALLY in the CFTRY block. Well, CFScript differs in this respect, so don't get caught by this. With CFScript, you do not place your finally statement in the try block. Instead, you place it after the try block.

Usually you will use a try block followed by a catch block or a series of catch blocks. Occasionally you will want to use a finally block.

In the CFScript catch block, the exceptionVariable variable contains the exception type. This variable is the equivalent of the CFCATCH tag, which is a cfcatch.Type built-in variable.

The finally statement and block always executes! This is something that developers often overlook. If your catch block catches an exception, the finally block executes after the catch block; otherwise, the finally block executes after the try block. This is really important to remember in that many developers get confused and think that a finally statement will not execute if there is an exception. Thus, they get some unexpected results. The finally statement is very useful and should be used where you have to have some code executed regardless of an exception. Some of the cases in which this would apply include alerts, logging, or the closing of a file.

Let's look at an example that we are going to use in a later chapter (Chapter 20, "Advanced XML"). One problem with ColdFusion MX's CFXML tag is that it does not let you validate a Document Type Definition (DTD). This can be very important when you are working with trading partners, content suppliers, or anyone else's XML that you cannot directly control.

To get around this issue, we are going to access the Apache eXtensible Markup Language (XML) parsers that ColdFusion uses directly via CFOBJECT. Because we are such good coders, we are going to make sure that we use exception handling! Now our code is rather well-commented, so we won't get into the specifics of it outside of how we are going to use exception handling. Our code is going to access the Java-based XML parser using the CreateObject function. The catch statement executes only if the CreateObject function generates an exception. The displayed information includes the exception message; the except.Message variable is the equivalent of calling the Java getMessage method on the returned Java exception object.

 <cfscript>      /**       * Xerces DOM Parsing       *       * Note, once the DOM document is created, from the initial Xerces parse       * of the XML document, accessing the XML elements/attributes is       * determined by the user.       */      // Create a DOM Parser       try       {      DOMParser = CreateObject("JAVA", "org.apache.xerces.parsers.DOMParser");      // Create blank DOM Document to hold parsed XML      document = CreateObject("JAVA", "org.w3c.dom.Document");      // Set features on parser      DOMParser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes",  false);      DOMParser.setFeature("http://apache.org/xml/features/dom/include-ignorable- whitespace", false);      // Validate XML against a DTD      DOMParser.setFeature( "http://xml.org/sax/features/validation", true);      // Parse document     //  make sure you out in the path to your xmlDocument      // for example, parsing a document directly…         //DOMParser.parse("http://department13.com/dev/xml/purchaseorder.xml");      DOMParser.parse(xmlDocument);      // Place parsed XML document in variable      document = DOMParser.getDocument();      // Now a DOM document containing the XML document has been created.      rootElement = CreateObject("JAVA","org.w3c.dom.Node");      rootElement = document.getDocumentElement();      WriteOutput("Our Root Node's name is: " & rootElement.getNodeName() );       }       catch("Any" excpt)  {  WriteOutput("The application was unable to perform a required operation.  Please try again later.<br>If this problem persists, please contact  the site Administrator and include the following information:<br>  #excpt.Message#<br>");  }</cfscript>  


Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579

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