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.
Содержание ColdFusion MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...