|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS BF on CF To Err Is Human, to Gracefully Handle Errors Is Divine
To Err Is Human, to Gracefully Handle Errors Is Divine
By: Ben Forta
Mar. 11, 2002 12:00 AM
Have you ever seen a ColdFusion error page - the bordered box on a white background that basically says someone (or something) messed up? I'm sure you have, as I'm sure your users have too. Errors, and error messages, are an unfortunate fact of development life. But while you may have to live with occasional errors, you definitely do not (and should not) have to live with that too-oft-seen CF error screen.
Prettier Error Pages
The simple solution is to use the <CFERROR> tag, which lets you replace the standard error screens with ones of your own. For example: <!--- Specify error page --->Once this code is executed, the standard.cfm file (in the error directory) will be displayed instead of the default error screen. Within the file itself any and all client technologies may be used (HTML, JavaScript, DHTML, images, etc.). However, CFML may not be used within <CFERROR> files. After all, if an error condition has occurred, allowing the execution of CFML could in turn generate an additional error, causing the code to execute again, generating yet another error causing...you get the idea. So no CFML tags or functions. Instead, a special set of variables is made available to you within error pages, and you can place them in your code to refer to things like:
<CFERROR> can also be used to define other error screens, for example, the screen displayed when form validation fails. Note: <CFERROR> needs to be executed on each request, so the ideal location for this tag is the Application.cfm page.
Understanding Exception Handling
That's where exception handling comes into play, with an emphasis on handling. Exception handling is a mechanism by which errors (or other conditions) may be trapped and then responded to. Using exception-handling error conditions doesn't halt processing, instead processing is diverted to special blocks of code that you can write - code that can do just about anything you need.
Try and Catch
start code blockAny code in which exception handling is to be used must be flagged - the start and end so noted. Exception-handling code is then placed at the end of the code block. Then, if an exception occurs within your code, processing is stopped and control is transferred to the error-handling code. The CFML code looks like this: <CFTRY><CFTRY> and </CFTRY> delimit the code in which exceptions are to be trapped, and ColdFusion tries (and thus <CFTRY>) to execute it. If an exception occurs (this is actually referred to as an error being thrown), it's caught by the exception-handling code (and thus <CFCATCH>). For example, if you want to send an e-mail to an administrator when an exception is thrown, you could do something like this: <CFTRY>The <CFCATCH> block here contains a <CFMAIL> tag, so when an exception is thrown, an e-mail message is sent to the specified address and processing continues (to whatever code comes next).
Handling Specific Exceptions
Look at the following code snippet: <CFTRY>Here four catch blocks are used. The first catches failed <CFINCLUDE> tags, the second catches database problems (including ODBC and SQL errors), the third catches <CFLOCK> errors (including timeouts and missing locks), and the fourth catches all other errors. It's a good idea to always have one <CFCATCH> with TYPE="any" to catch any exceptions not specifically caught, and to specify it last (otherwise it'll catch exceptions that you might not want it to). Other exception types are supported, including over 50 advanced types (used to catch very specific exceptions, like <CFPOP> authentication failures) and custom types used to define exception types of your own.
Using Exception Handling
And that's just the start of it.
Summary
Neo, which I'll be covering extensively starting next month, extends ColdFusion's try/catch handling even further. If you have yet to play with these tags, there couldn't be a better time to start. YOUR FEEDBACK CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||