
312
Chapter 14: Handling Errors
</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="CompanyInfo">
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=
"CompanyInfoBackup"
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.
<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.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......