So I ran across an article not too long ago, can’t remember where, it was one of those things that just gets passed around, about the importance of try/catch/finally… and how the finally block is executed no matter how the controlling block is terminated. So I put together a toy example to test something out, and thought it was a little cool. Consider the following java method:
public boolean trytest( final int x ) {
switch( x ) {
case 0:
try { return true; }
finally { break; }
default:
return true;
}
return false;
}
Now if you were to call this method and pass it 0
, what would happen is the try
block would return true. Since the controlling block exited, the finally
block is hit and the break statement executes, dropping control out of the switch
statement so that the return false
line statement is executed. The method returns false.
So there ya go. Be careful what you do in your finally’s; you can easily override the behavior of your try if you’re not paying attention.