User Guide

Table Of Contents
Handling runtime exceptions with ColdFusion tags 337
Testing the code
To test the various ways errors can occur and be handled in this example, try the following:
In the calling page, change the attribute name to any other value; for example, My Attrib.
Then change it back.
In the first cfquery tag, change the data source name to an invalid data source; for example,
NoDatabase.
With an invalid first data source name, change the data source in the second cfquery tag to
cfdocexamples.
Insert cfthrow tags throwing custom exculpations in various places in the code and observe
the effects.
<cfcatch type = "Database" />
If the second database query failed with a
database error, gives up silently. Because the
Database type
cfcatch tag does not have a
body, the tag exits. The calling page does not
get a
getEmpsResult variable. It cannot tell
whether the database had no match or an
unrecoverable database error occurred, but it
does know that no match was found.
<cfcatch type = "Any">
<cfrethrow>
</cfcatch>
</cftry>
If the second database query failed for any other
reason, throws the error up to the next try block.
Ends the innermost try block
<cfelse>
<cfrethrow>
</cfif>
</cfcatch>
In the second try block, handles the case in
which the first database query failed for a reason
other than a failure to find the database.
Rethrows the error up to the next level, the
outermost try block.
<cfcatch type = "Any">
<cfrethrow>
</cfcatch>
</cftry>
In the second try block, catches any errors other
exceptions and rethrows them up to the
outermost try block.
Ends the second try block.
<cfcatch Type = "Any">
<h2>Sorry</h2>
<p>An unexpected error happened in
processing
your user inquiry.
Please report the following to
technical support:</p>
<cfoutput>
Type: #cfcatch.Type#
Message: #cfcatch.Message#
</cfoutput>
<cfexit method = "exittag">
</cfcatch>
</cftry>
</cfif>
In the outermost try block, handles any
exceptions by displaying an error message that
includes the exception type and the exception’s
error message. Because there was no code to
try that is not also in a nested try block, this
cfcatch tag handles only errors that are rethrown
from the nested blocks.
Exits the custom tag and returns to the calling
page.
Ends the catch block, try block, and initial
cfif
block.
Code Description