Adobe COLDFUSION 9 Manual page 305

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
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>
<!--- Try to query the main database and set a caller
variable to the result --->
<cfquery Name = "getUser" DataSource="cfdocexamples">
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="cfdocexamplesBackup">
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>
<cfcatch type = "Any">
<cfrethrow>
</cfcatch>
</cftry>
<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>
Description
Makes sure the calling page specified an
not, throws a custom error that indicates the problem and exits
the tag. The calling page handles the thrown error.
If the tag has an
inside an outermost try block. The
handles any 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
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
SQLState
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
If the second database query failed with a database error, gives
up silently. Because the Database type
have a body, the tag exits. The calling page does not get a
getEmpsResult
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.
In the second try block, catches any errors other exceptions and
rethrows them up to the outermost try block.
Ends the second try block.
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
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
Last updated 8/5/2010
EmpName
EmpName
attribute, does the remaining work
cfcatch
block at its end
variable with the query results.
variable value of S100 or IM002).
variable with the query results.
cfcatch
variable. It cannot tell whether the database
cfif
300
attribute. If
tag does not
cfcatch
tag
block.

Advertisement

Table of Contents
loading

Table of Contents