Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 303

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
Reviewing the code
The following table describes the code:
Code
<cftry>
<cf_getEmps EmpName="Jones">
<cfcatch type="myApp.getUser.noEmpName">
<h2>Oops</h2>
<cfoutput>#cfcatch.Message#</cfoutput><br>
</cfcatch>
<cfif isdefined("getEmpsResult")>
<cfdump var="#getEmpsResult#">
</cfif>
The custom tag page
The custom tag page searches for the name in the database and returns any matching records in a getEmpsResult
variable in the calling page. It includes several nested
see Reviewing the code section, following the example:
Save the following code as getEmps.cfm in the same directory as the calling page.
<!--- If the tag didn't pass an attribute, throw an error to be handled by
the calling page --->
<cfif NOT IsDefined("attributes.EmpName")>
<cfthrow Type="myApp.getUser.noEmpName"
message = "Last Name was not supplied to the cf_getEmps tag.">
<cfexit method = "exittag">
<!--- Have a name to look up --->
<cfelse>
<!--- Outermost Try Block --->
<cftry>
<!--- Inner Try Block --->
<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>
<!--- If the query failed with a database error, check the error type
to see if the database was found --->
<cfcatch type= "Database">
<cfif (cfcatch.SQLState IS "S100") OR (cfcatch.SQLState IS
"IM002")>
<!--- If the database wasn't found, try the backup database --->
<!--- Use a third-level Try block --->
<cftry>
cftry
<cfquery Name = "getUser" DataSource="cfdocexamplesBackup">
SELECT *
FROM Employee
WHERE LastName = '#attributes.EmpName#'
</cfquery>
Last updated 1/20/2012
Description
In a
block, calls the cf_getEmps custom tag
cftry
(getEmps.cfm).
If the tag throws an exception indicating that it did not receive
a valid attribute, catches the exception and displays a message,
including the message variable set by the
custom tag.
If the tag returns a result, uses the
production application would not use the
blocks to handle error conditions. For a full description,
298
cfthrow
tag in the
tag to display it. (A
cfdump
tag.)
cfdump

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents