I was in a code review earlier last week, and someone said that they didn’t like using <cftry> and <cfcatch>. When we as a group investigated further it was revealed that they had used it “incorrectly,” experienced problems for doing so, and labeled the try catch model as bad, instead of the implementation. So I figured I would quickly go over my opinions of what you should be using them for.
To start with, the incorrect usage of <CFTry> was wrapping it around a troublesome page, so instead of visibly erroring, the page would just silently fail. To add insult to injury, there was an error in the cfcatch block which caused the whole thing to throw an application level error anyway. This was, to put it mildly, a flawed use of <cftry> and <cfcatch>.
In a nutshell, the <cftry> and <cfcatch> model in ColdFusion is for handling expected errors, as opposed to handling unexpected errors. ColdFusion has a pretty good system for handling unexpected errors – a combination of <cferror> tags, and use of onError in the application.cfc. So wrapping an entire page in <cftry> to catch any error that may come up is not only the wrong thing to do, it’s also pretty redundant.
In my opinion <cftry> and <cfcatch> should be used to handle errors around small pieces of code that have the potential to error in predictable ways. For example here is a line of code from one of my applications:
cfset Wsdl = “<webservice URL>” />
<cfset displayBrainstorm = FALSE />
<cftry>
<cfinvoke timeout=“1” webservice=“#Wsdl#” method=“AuthorInfo” returnvariable=“BrainstormStruct”>
<cfinvokeargument name=“username” value=“#url.username#” />
</cfinvoke>
<cfset displayBrainstorm = BrainstormStruct.IsAuthor />
<cfcatch type=“any”>
<cfif FindNoCase(“stub objects”, cfcatch.Message)>
<cfset createObject(“java”,“coldfusion.server.ServiceFactory”) .XmlRpcService.refreshWebService(Wsdl) />
<cftry>
<cfinvoke timeout=“1” webservice=“#Wsdl#” method=“AuthorInfo” returnvariable=“BrainstormStruct”>
<cfinvokeargument name=“username” value=“#url.username#” />
</cfinvoke>
<cfset displayBrainstorm = BrainstormStruct.IsAuthor />
<cfcatch type=“any”>
<cfrethrow />
</cfcatch>
</cftry>
<cfelse>
<cfrethrow />
</cfif>
</cfcatch>
</cftry>
In it you’ll notice I’m doing a webservice call. In my experience webservices can have issues with the skeleton ColdFusion creates to consume them, especially if details about the webservice change. However, my application has little control over this webservice. So I wrap the call to the webservice in a <cftry> block, and if my expected error condition occurs, (something with to do the skeleton, or “stub objects”) I reset the webservice and try again.
You’ll notice that if it isn’t a “stub objects” problem, it assumes that it is some other issue, and rethrows the error so the application error handlers can tackle the problem. Why, because it is an unexpected problem, not an expected one.
In conclusion, I’m not speaking for all programming languages, but at least in ColdFusion <cftry> and <cfcatch> should be used to handle error that you can predict. The shouldn’t be your default error handling technique, and like a <Cflock> or a <cftransaction> they should surround as little code as possible.
You neglected to mention the article your beautiful wife sent you that pointed out how an improperly used try/catch is a security breach waiting to happen, ala this week’s animated cursor boondoggle:
Tales from the Crypto: Don’t catch exceptions
An improperly used try/catch causes errors to fail silently, which just gives a malicious user the opportunity to try and break your program repeatedly without consequence. It’s saying “Oh, did your buffer overflow fail? Here try again, I don’t mind.”
That’s great if you know you’re perfect, and your code is infalible. However, if that’s the case, you shouldn’t need the try/catch at all in the first place!
LikeLike
Hey, you stripped out my URL. The article is at:
http://msmvps.com/blogs/alunj/archive/2007/04/02/don-t-catch-exceptions.aspx
LikeLike
The docs give a good overview of all this and support your position:
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/js/html/wwhelp.htm?href=part_cfm.htm
LikeLike
I like this article.
I particularly like the one line:
“In a nutshell, the and model in ColdFusion is for handling expected errors, as opposed to handling unexpected errors.”
This explains the crux of my objection to exception-based programming – that it is poorly used by developers, who see it as their job to somehow manage every exception that comes in.
Instead, they should be taught that they should handle only those things that they can correctly recover from at all times.
I take exception, if you’ll pardon the pun, at the use of “exception” to describe part of the normal operation of an application – but more at the expectation by a generation of developers that exceptions are the rule, and that they “gotta catch ’em all”.
LikeLike
Thanks, Alun. As you can see Janice likes your blog.
LikeLike
http://www.blackhatworld.com/images/clip_image002.gif
Good Afternoon http://es5.com/images/smile.gif
We are doing research online WhiteHat SEO traffic software to enlarge our SEO rankings. I’d be thankful for any and all form of SEO and Web Site promotion
If you know anybody that will to support quality traffic and with Whitehat Methods. I want to learn how to promote and exploit of our websites & products.
LikeLike