Adobe ColdFusion 8

Using the cfrethrow tag

The cfrethrow tag lets you create a hierarchy of error handlers. It tells ColdFusion to exit the current cfcatch block and "rethrow" the exception to the next level of error handler. Thus, if an error handler designed for a specific type of error cannot handle the error, it can rethrow the error to a more general-purpose error handler. The cfrethrow tag can only be used in a cfcatch tag body.

The cfrethrow tag syntax

The following pseudocode shows how you can use the cfrethrow tag to create an error-handling hierarchy:

<cftry>
    <cftry>
        Code that might throw a database error
        <cfcatch Type="Database">
            <cfif Error is of type I can Handle>
                Handle it
            <cfelse>
                <cfrethrow>
            </cfif
        </cfcatch>
    </cftry>
    <cfcatch Type="Any">
        General Error Handling code
    </cfcatch>
</cftry>

Although this example uses a Database error as an example, you can use any cfcatch type attribute in the innermost error type.

Follow these rules when you use the cfrethrow tag:

  • Nest cftry tags, with one tag for each level of error handling hierarchy. Each level contains the cfcatch tags for that level of error granularity.
  • Place the most general error catching code in the outermost cftry block.
  • Place the most specific error catching code in the innermost cftry block.
  • Place the code that can cause an exception error at the top of the innermost cftry block.
  • End each cfcatch block except those in the outermost cftry block with a cfrethrow tag.