Published:
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.
Back when ColdFusion 9 was releashed the cftry/cfcatch tags were enhanced to include a new tag <cffinally>. While I have been aware of it, up to now I have not used it simply because I did not understand why or where it would be useful.
What <cffinally> does (According to Adobe)
If you read about the tag in the adobe live docs: <cffinally> you get this explanation:
Used inside a cftry tag. Code in the cffinally block is processed after the main cftry code and, if an exception occurs, the cfcatch code. The cffinally block code always executes, whether or not there is an exception.
Big deal so code inside the cffinally block runs in the end regardless of if there is an error that throws the catch, why is this useful? According to this explanation the following two code blocks behave identically:
Using <cffinally>
<cfset cat = "">
<cftry>
....
<!--- Do something that breaks the code --->
<cfset cat = dog>
<cfcatch>
<!--- Handle the error --->
....
</cfcatch>
<cffinally>
<!--- This code will run no matter what --->
<cfset dologging("log message")>
</cffinally>
</cftry>
Not using <cffinally>
<cfset cat = "">
<cftry>
....
<!--- Do something that breaks the code --->
<cfset cat = dog>
<cfcatch>
<!--- Handle the error --->
....
</cfcatch>
</cftry>
<!--- This code will run no matter what --->
<cfset dologging("log message")>
These two blocks of code do in fact behave identically in this scenario and because of Adobe's less than stellar documentation I never knew about <cffinally>'s hidden power.
What <cffinally> actually does
So I was reading up on another language when I came across that language's documentation for its version of try/catch/finally.
In the documentation it stated that:
Code inside a "finally" block gets executed after the code in the try/catch block regardless of if there was an error thrown to execute the catch block and regardless of if an error occurs in the catch block.
Ding, ding, ding, ding, light bulb moment. So of course the moment I read that I had to go see if this also holds true for ColdFusion and guess what, it does! When you run this code you will get the below output:
<cfset cat = "">
<cftry>
<!--- Do something that breaks the code --->
<cfset cat = dog>
<cfoutput>#cat#<br/></cfoutput>
<cfcatch>
<!--- Handle the error --->
Error thrown: Do something clever to try fix it<br/>
<!--- Do something in the catch that breaks the error handling --->
<cfset cat = bird>
</cfcatch>
<cffinally>
<!--- This code will run no matter what --->
Finally block executed regardless of any errors in the try or catch block
</cffinally>
</cftry>
Finally block executed regardless of any errors in the try or catch block
{An error message that "Variable BIRD is undefined."}
So there you have it, you should use <cffinally> whenever you must run some post processing after a block of code is executed which can throw an error and where the error handling code inside your catch may also throw an error.
Reader Comments
@BenNadel
Tuesday, May 6, 2014 at 8:03:55 AM Coordinated Universal Time
I was actually just looking into this stuff this morning. I love the Finally tag; but, had a doubt about what would happen if the catch tag threw an error. This is so comforring.