Ask a Jedi: Error thowing and cfthrow

Mike asks,

Is there an order you have to error catching? I want to use <cfthrow> in my CFC for an expression if my database columns don't equal the values. But I also have a <cferror> in my Application.cfm and that has a exception tied to it and that takes precedence. How do I get my <cfthrow> to get thrown, or have certain errors take higher precedence?

If I understand you right, you are perhaps getting confused about how cferror relates to error handling and cfthrow. This is how I would explain it.

The cferror tag (and onError method) are used for global error handling of your application. I call this the “On Crap” handler (except I don’t say crap). Every application should have one of these to hide errors from the user, and at minimum, it should email the details of the error to the administrator. I see far too many sites not doing this which is sad since it takes about five minutes to set up. (I’ve been guilty of this as well.)

So this would handle exceptions at a global level of your application. Another way of handling errors is with cftry/cfcatch. This will process a set of code (inside the cftry block) and if an exception is thrown, cfcatch can handle it. So if you want to handle the exception thrown by the CFC, you would want to wrap the call with a try/catch block:

<cftry> <cfset foo = someCFC.enter(4,8,15,16,23,32)> <cfcatch> <cfoutput>An error occured entering the numbers. </cfcatch> </cftry>

The cfcatch tag can also be tuned to only catch certain exceptions. The code above would catch any exception, but you could change it to specifically look for the exception that your CFC throws. That way if something else goes wrong, your global error handler would handle it instead.

This is just a super high level look at exception handling. I’d check out the docs on error handling here:

http://livedocs.macromedia.com/coldfusion/7/htmldocs/00001130.htm#1220254

p.s. Note, I’m having a lot of trouble with livedocs, which I’m sure is due to the recent changes. So if you have trouble, maybe check your local documentation.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments