User Guide

Table Of Contents
290 Chapter 13: Designing and Optimizing a ColdFusion Application
Using the onRequestEnd method
You use the
onRequestEnd method for code that should run at the end of each request. (In
ColdFusion versions through ColdFusion MX 6.1, you would use the OnRequestEnd.cfm page
for such code.) Typical uses include displaying dynamic footer pages. For an example, see
onSessionEnd in CFML Reference.
Note: If you do not include an onRequest method in Application.cfm file, the onRequestEnd method
does not share a Variables scope with the requested page, but it does share Request scope variables.
Handling errors in Application.cfc
The following sections briefly describe how you can handle errors in Application.cfc. For more
information on error pages and error handling, see Chapter 14, “Handling Errors,on page 307.
For details on implementing the
onError method, see onError in CFML Reference.
Application.cfc error handling techniques
Application.cfc can handle errors in any combination of the following ways:
You can use try/catch error handling in the event methods, such as onApplicationStart or
onRequestStart, to handle exceptions that happen in the event methods.
You can implement the onError method. This method receives all exceptions that are not
directly handled by try/catch handlers in CFML code. The method can use the
cfthrow tag to
pass any errors it does not handle to ColdFusion for handling.
You can use cferror tags in the application initialization code following the cfcomponent tag,
typically following the code that sets the applications This scope variables. These tags specify
error processing if you do not implement an
onError method, or if the onError method
throws an error. You could implement an application-specific validation error handler, for
example, by putting the following tag in the CFC initialization code:
<cferror type="VALIDATION" template="validationerrorhandler.cfm">
The ColdFusion MX default error mechanisms handle any errors that are not handled by the
preceding techniques. These mechanisms include the site-wide error handler that you can
specify in the ColdFusion MX Administrator and the built-in default error pages.
These techniques let you include application-specific information, such as contact information or
application or version identifiers, in the error message, and let you display all error messages in the
application in a consistent manner. You can use Application.cfc to develop sophisticated
application-wide error-handling techniques, including error-handling methods that provide
specific messages, or use structured error-handling techniques.
Note: The onError method can catch errors that occur in the onSessionEnd and onApplicationEnd
application event methods. It will not display messages to the user, however, because there is no
request context. The
onError function can log errors that occur when the session or application ends.
Handling server-side validation errors in the onError method
Server-side validation errors are actually ColdFusion exceptions; as a result, if your application
uses an
onError method, this method gets the error and must handle it or pass it on to
ColdFusion for handling.