Adobe COLDFUSION 9 Manual page 302

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

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
<cftry>
<cftry>
Code that might throw a database error
<cfcatch Type="Database">
<cfif Error is of type I can Handle>
Handle it
<cfelse>
<cfrethrow>
</cfif
</cfcatch>
</cftry>
<cfcatch Type="Any">
General Error Handling code
</cfcatch>
</cftry>
Although this example uses a Database error as an example, you can use any
error type.
Follow these rules when you use the
• Nest
tags, with one tag for each level of error handling hierarchy. Each level contains the
cftry
that level of error granularity.
• Place the most general error catching code in the outermost
• Place the most specific error catching code in the innermost
• Place the code that can cause an exception error at the top of the innermost
• End each
block except those in the outermost
cfcatch
Example: using nested tags, cfthrow, and cfrethrow
The following example shows many of the discussed techniques including nested
tags. The example includes a simple calling page and a custom tag page:
cfrethrow
• The calling page does little more than call the custom tag with a single attribute, a name to be looked up in a
database. It does show, however, how a calling page can handle an exception thrown by the custom tag.
• The custom tag finds all records in the cfdocexamples database with a matching last name, and returns the results
in a Caller variable. If it fails to connect with the main database, it tries a backup database.
The calling page
The calling page represents a section from a larger application page. To keep things simple, the example hard-codes
the name to be looked up.
<cftry>
<cf_getEmps EmpName="Jones">
<cfcatch type="myApp.getUser.noEmpName">
<h2>Oops</h2>
<cfoutput>#cfcatch.Message#</cfoutput><br>
</cfcatch>
</cftry>
<cfif isdefined("getEmpsResult")>
<cfdump var="#getEmpsResult#">
</cfif>
tag:
cfrethrow
cftry
cftry
cftry
Last updated 8/5/2010
type attribute in the innermost
cfcatch
block.
block.
block.
cftry
block with a
tag.
cfrethrow
blocks and the
cftry
297
tags for
cfcatch
and
cfthrow

Advertisement

Table of Contents
loading

Table of Contents