CFUse CFTry CFCatch

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.

6 thoughts on “CFUse CFTry CFCatch

  1. 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!

    Like

  2. 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”.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s