
Handling runtime exceptions with ColdFusion tags
309
For example, on a form action page or custom tag used to set a password, the application can
determine whether the password entered is a minimum length, or contains both letters and
number, and throw an error with a message that indicates the password rule that was broken. The
cfcatch
block handles the error and tells the user how to correct the problem.
Using the cfrethrow tag
The
cfrethrow
tag lets you create a hierarchy of error handlers. It tells ColdFusion to exit the
current
cfcatch
block and "rethrow" the exception to the next level of error handler. Thus, if an
error handler designed for a specific type of error cannot handle the error, it can rethrow the error
to a more general-purpose error handler. The
cfrethrow
tag can only be used in a
cfcatch
tag
body.
The cfrethrow tag syntax
The following pseudo-code shows how you can use the
cfrethrow
tag to create an error-handling
hierarchy:
<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
cfcatch
type
attribute in the innermost error type.
Follow these rules when you use the
cfrethrow
tag:
•
Nest
cftry
tags, with one tag for each level of error handling hierarchy. Each level contains the
cfcatch
tags for that level of error granularity.
•
Place the most general error catching code in the outermost
cftry
block.
•
Place the most specific error catching code in the innermost
cftry
block.
•
Place the code that can cause an exception error at the top of the innermost
cftry
block.
•
End each
cfcatch
block except those in the outermost
cftry
block with a
cfrethrow
tag.
Example: using nested tags, cfthrow, and cfrethrow
The following example shows many of the techniques discussed in this chapter, including nested
cftry
blocks and the
cfthrow
and
cfrethrow
tags. The example includes a simple calling page
and a custom tag page:
•
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.
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......