Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with:

CFFinally Will Execute Even If CFCatch Throws An Error In ColdFusion

By Ben Nadel on
Tags: ColdFusion

I use the CFFinally tag a lot in ColdFusion as a means to clean up the state of my code in a context that may raise an exception. As of late, I've had a nagging doubt about my understanding of the way that CFFinally and CFCatch interact. As such, I wanted to run a quick sanity check to make sure that the CFFinally tag would still execute even if the CFCatch tag threw an exception.

The test was simple, log the execution of the CFTry, CFCatch, and CFFinally tags in which the CFCatch tag re-throws an error:

  • <cfscript>
  •  
  • logfile = fileOpen( expandPath( "./log.txt" ), "append" );
  •  
  • try {
  •  
  • fileWriteLine( logfile, "In Try" );
  •  
  • throw( type = "TryError" );
  •  
  • } catch ( any error ) {
  •  
  • fileWriteLine( logfile, "In Catch" );
  •  
  • rethrow;
  •  
  • } finally {
  •  
  • fileWriteLine( logfile, "In Finally" );
  •  
  • fileClose( logfile );
  •  
  • }
  •  
  • </cfscript>

When we run this code, we get the following logfile output:

In Try
In Catch
In Finally

As you can see, the CFFinally tag logged output even when the CFCatch threw an error (which was unhandled in the application, by the way).

Ok, well that eases my mind. I guess the CFFinally tag executes no matter what. Which is tote's awesome.




Reader Comments

Hi @Ben,

That's what CFFinally is for, but that's okay. It never hurts to write experiment pages. They're always so much more reassuring than accepting some human's word for it.

In ye olden days, experiment pages were called "sleuthing". In most systems I work on, there's usually a subdirectory somewhere filled to the gills with files named SleuthNestedCFTry.cfm, SleuthFrameOnLoad.cfm, SleuthSessionJavaObjects.cfm, etc.

I always forget why "finally" needs to exist. It seems as if you could just put the code after a try/catch block, right? I just looked it up to remind myself and discovered something ├╝ber surprising. According to http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html the finally gets done <u>even if the statement after the try/catch/finally ISN'T done</u>.

Now THAT makes me want to write another experiment page.

Reply to this Comment

@Steve,

I have various testing folders, with loads of stuff like this. I wouldn't even be surprised IF I had ALREADY tested this as some point. But, eventually, you do something the same over and over again and sometimes you forgot the little details for the outlier cases.

Wanna try something really odd - try putting a RETURN statement in your Finally block. At least in JavaScript, it will override the Return statement in your Try block... mind-blown.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.