ColdFusion CFCatch Tags Catch Exceptions Based On Type Hierarchy

Posted June 11, 2008 at 8:25 AM by Ben Nadel

Tags: ColdFusion

I learned an interesting fact from Brett last night at the New York ColdFusion User Group - ColdFusion's CFThrow / CFCatch combination works in a hierarchical fashion based on the dot-notation of the exception type being thrown. What this means is that as you add "dot-something" to an exception type, it becomes more specific. For example, my blog might throw a bunch of "kinky" exceptions:

Kinky

(This is all theoretical - my blog doesn't throw custom exceptions) However, if an error happens in the blog, it might throw a blog exception:

Kinky.BlogException

This is a really general to the blog section, but even in there, we can get more specific:

Kinky.BlogException.EntryNotFound

EntryNotFound is a type of BlogException which is a type of Kinky exception. That's what I mean when I say hierarchical.

Now, when it comes to catching errors that bubble up through the pipeline, most people just use a single CFCatch tag to catch all errors. You can, however, specify the type of error that gets caught by a specific CFCatch tag. This allows you have multitple CFCatch tags in a single CFTry / CFCatch statement, each of which is listening for a different type.

All that stuff, I already knew; what was new to me was that the CFCatch tag will catch errors based on the exception hierarchy. What I mean by this is that if you have a CFCatch tag that is listening for exceptions of type "Kinky", it will catch exceptions of type "Kinky" as well as type "Kinky.BlogException" because "Kinky.BlogException" is a sub-class (if you will) of the "Kinky" exception type.

Let's run a quick demo to see CFCatch in action:

  • <cftry>
  •  
  • <cfswitch expression="#RandRange( 1, 4 )#">
  • <cfcase value="1">
  •  
  • <!--- Throw exception. --->
  • <cfthrow type="Kinky" />
  •  
  • </cfcase>
  • <cfcase value="2">
  •  
  • <!--- Throw exception. --->
  • <cfthrow type="Kinky.BlogException" />
  •  
  • </cfcase>
  • <cfcase value="3">
  •  
  • <!--- Throw exception. --->
  • <cfthrow type="Kinky.BlogException.EntryNotFound" />
  •  
  • </cfcase>
  • <cfcase value="4">
  •  
  • <!--- Throw exception. --->
  • <cfthrow type="Kinky.InvalidUserAgent" />
  •  
  • </cfcase>
  • </cfswitch>
  •  
  •  
  • <!--- Catch all blog exceptions. --->
  • <cfcatch type="Kinky.BlogException">
  • <cfoutput>
  •  
  • <p>
  • #CFCATCH.Type#<br />
  • Kinky.BlogException
  • </p>
  •  
  • </cfoutput>
  • </cfcatch>
  •  
  • <!--- Catch all kinky unhandled kinky errors. --->
  • <cfcatch type="Kinky">
  • <cfoutput>
  •  
  • <p>
  • #CFCATCH.Type#<br />
  • Kinky
  • </p>
  •  
  • </cfoutput>
  • </cfcatch>
  •  
  • </cftry>

Notice that in this demo, we are throwing four possible exceptions, but only catching two types of exceptions. Running this code a few times, we get the following output:

Kinky.BlogException
Kinky.BlogException

Kinky.InvalidUserAgent
Kinky

Kinky
Kinky

Kinky.BlogException.EntryNotFound
Kinky.BlogException

Kinky.InvalidUserAgent
Kinky

Kinky.BlogException
Kinky.BlogException

Kinky.BlogException.EntryNotFound
Kinky.BlogException

Notice that the even though we had only two CFCatch tags, we were able to catch all four thrown exception types. This is because our CFCatch tags were catching general exception types which covered both the general and the more specific.

This seemed pretty cool to me. Overall, I have not really made much use of custom exceptions and exception handling, but the more I see it, the more it is something that I want to get into.



Reader Comments

Jun 11, 2008 at 1:55 PM // reply »
16 Comments

After all the tidbits I've learned from this blog over they years I'm glad I was able to give back. I've always enjoyed the way you get under the hood on your posts.

I use cfthrow *a_lot* and I've learned some other tricks and techniques that really come in handy. Perhaps I can follow on at another NYCFUG in the future.

cheers,

.brett


Jun 12, 2008 at 7:39 AM // reply »
11,238 Comments

@Brett,

I am slowly getting more and more into exception usage. For the longest time, I had this weird feeling like I had to protect my users; I was always drying to recover from invalid data formats that would *only* occur if someone changed the HTML (ex. Altering a SELECT menu to return non-numeric values). For some reason, I felt the need to NOT give errors to people who were malicious.

But, that is changing. I no longer feel like I need to protect people from themselves. I am getting more comfortable with presenting error pages when people forcibly cause errors.

But, more than that, I am finding exceptions very useful for lots of other things. Right now, I am using them heavily within a web service API that I have for an AJAX application - the entire API is wrapped in CFTry / CFCatch and the different sub-parts can throw exceptions that the API will catch and return a more meaningful API response.

Anyway, cool stuff.


Don
May 14, 2009 at 1:03 PM // reply »
57 Comments

I'm nibbling around the edges on this. It seems that exceptions could be a great tool, but I'm not quite seeing how I can integrate them. Do you have any other posts regarding their use? Especially custom exceptions.


May 19, 2009 at 9:42 AM // reply »
11,238 Comments

@Don,

I have started to use exceptions more in validation; but, I am nowhere near having anything that I'm happy with. Still working on it.


Jan 15, 2013 at 1:24 PM // reply »
2 Comments

cfCatch uses different struct keys (not really struct keys but they look like struct keys) based on what the cfCatch.type attribute is.

I like to build nice, well layed out error emails to parties involved. How do I know what keys are passed given any particular cfCatch.type key?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools