CFFinally Will Execute Even If CFCatch Throws An Error In 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.
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 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.
@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.
@WMW @Ben ...
I couldn't figure out a good use case for finally until I ran into a great example for handling JMS connections ... ( JMS 1.0 has a lot of boilerplate connection code... )
Example...
https://gist.github.com/EdwardBeckett/af9bfeaddb87a17d786d
Ok, one more sanity check - CFFinally and CFAbort interactions:
www.bennadel.com/blog/2661-cffinally-tag-will-execute-even-after-an-abort-in-coldfusion.htm
I just like to cross my T's and dot my I's.