User Guide

Table Of Contents
336 Chapter 14: Handling Errors
Handle all unhandled exceptions, including rethrown exceptions, by
displaying a message and exiting to the calling page.--->
<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>
Reviewing the code
The following table describes the code:
Code Description
<cfif NOT IsDefined("attributes.EmpName")>
cfthrow Type="myApp.getUser.noEmpName"
message = "Last Name was not supplied to
the cf_getEmps tag.">
<cfexit method = "exittag">
Makes sure the calling page specified an
EmpName attribute. If not, throws a custom error
that indicates the problem and exits the tag. The
calling page handles the thrown error.
<cfelse>
<cftry>
If the tag has an EmpName attribute, does the
remaining work inside an outermost try block.
The
cfcatch block at its end handles any
otherwise-uncaught exceptions.
<cftry>
<cfquery Name = "getUser"
DataSource="cfdocexamples">
SELECT *
FROM Employee
WHERE LastName = '#attributes.EmpName#'
</cfquery>
<cfset caller.getEmpsResult = getuser>
Starts a second nested try block. This block
catches exceptions in the database query.
If there are no exceptions, sets the calling page’s
getEmpsResult variable with the query results.
<cfcatch type= "Database">
<cfif (cfcatch.sqlstate IS "S100") OR
(cfcatch.sqlstate IS "IM002")>
<cftry>
<cfquery Name = "getUser" DataSource=
"cfdocexamplesBackup"
SELECT *
FROM Employee
WHERE LastName =
'#attributes.EmpName#'
</cfquery>
<cfset caller.getEmpsResult = getuser>
If the query threw a Database error, checks to
see if the error was caused by an inability to
access the database (indicated by an
SQLState
variable value of S100 or IM002).
If the database was not found, starts a third
nested try block and tries accessing the backup
database. This try block catches exceptions in
this second database access.
If the database inquiry succeeds, sets the calling
page’s
getEmpsResult variable with the query
results.