MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual page 312

Developing coldfusion mx applications
Table of Contents

Advertisement

</cfoutput>
<cfexit method = "exittag">
</cfcatch>
</cftry>
</cfif>
Reviewing the code
The following table describes the code:
Code
<cfif NOT IsDefined("attributes.EmpName")>
<cfthrow Type="myApp.getUser.noEmpName"
message = "Last Name was not supplied to
the cf_getEmps tag.">
<cfexit method = "exittag">
<cfelse>
<cftry>
<cftry>
<cfquery Name = "getUser"
DataSource="CompanyInfo">
SELECT *
FROM Employee
WHERE LastName = '#attributes.EmpName#'
</cfquery>
<cfset caller.getEmpsResult = getuser>
<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>
<cfcatch type = "Database" />
<cfcatch type = "Any">
<cfrethrow>
</cfcatch>
</cftry>
<cfelse>
<cfrethrow>
</cfif>
</cfcatch>
312
Chapter 14: Handling Errors
Description
Makes sure the calling page specified an
attribute. If not, throws a custom error
EmpName
that indicates the problem and exits the tag. The
calling page handles the thrown error.
If the tag has an
EmpName
remaining work inside an outermost try block.
The
block at its end handles any
cfcatch
otherwise-uncaught exceptions.
Starts a second nested try block. This block
catches exceptions in the database query.
If there are no exceptions, sets the calling page's
variable with the query results.
getEmpsResult
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
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
variable with the query
getEmpsResult
results.
If the second database query failed with a
database error, gives up silently. Because the
Database type
cfcatch
body, the tag exits. The calling page does not
get a
variable. It cannot tell
getEmpsResult
whether the database had no match or an
unrecoverable database error occurred, but it
does know that no match was found.
If the second database query failed for any other
reason, throws the error up to the next try block.
Ends the innermost try block
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.
attribute, does the
SQLState
tag does not have a

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion mx

Table of Contents