Skip to main content
Ben Nadel at RIA Unleashed (Nov. 2010) with: Carol Loffelmann and Vicky Ryder
Ben Nadel at RIA Unleashed (Nov. 2010) with: Carol Loffelmann ( @Mommy_md ) Vicky Ryder ( @fuzie )

CFFinally Will Execute Even If CFCatch Throws An Error In ColdFusion

By on

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:


	logfile = fileOpen( expandPath( "./log.txt" ), "append" );

	try {

		fileWriteLine( logfile, "In Try" );

		throw( type = "TryError" );

	} catch ( any error ) {

		fileWriteLine( logfile, "In Catch" );


	} finally {

		fileWriteLine( logfile, "In Finally" );

		fileClose( logfile );



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.

Want to use code from this post? Check out the license.

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



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.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel