• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

19. Scripting > 19.4. Error and Exception Handling

Error and Exception Handling

New in ColdFusion MX is the ability to trap errors and exceptions within CFScript code using try/catch statements similar to the cftry and cfcatch tags described in Chapter 9. If you were writing exception-handling code using the cftry and cfcatch tags, it might look something like the code in Example 19-20.

Example 19-20. Exception handling with cftry and cfcatch
<cftry>
  <cfset x=y+1>
  
  <cfcatch type="Expression">
    <cfoutput>
    Expression Error: #cfcatch.Message#
    </cfoutput>
  </cfcatch>
   
  <cfcatch type="Any">
    <cfoutput>
    General Error: #cfcatch.Message#
    </cfoutput>
  </cfcatch>
</cftry>

This code attempts to set the variable x equal to y+1. It then includes cfcatch code for handling Expression type errors, and another cfcatch block for catching Any exception that is not caught by the Expression type. In this case, the exception handling comes in the form of a semi-custom error message written to the browser.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint