Clarification regarding finally blocks
I'm primarily a C++ programmer who has to work with Java code now and then. I'd like some clarification regarding when finally blocks will be executed.
Let's say I lock a mutex. Or Lock, as Java prefers to call the things. Anyway, I want to ensure that when the function exits the thing is unlocked, without having "unlock()" calls scattered through every catch block and at every return statement. (Let's say I didn't write this code and wouldn't have used so many return statements if I had.)
Can I just stick a finally block at the end of the function, and assume that it will be called no matter where the function returns from or what exceptions get thrown? Or does it have to follow a try block? Let's say:
Code:
lock.lock();
try {}
catch {
return;
}
try {}
catch{}
catch{}
finally{
lock.unlock();
}
will the finally apply even if there's a return statement in the first catch, which is part of a different try/catch structure?
In C++ I'd just put a scoped mutex lock on the stack and be done with it, but I'd like to learn the right way of doing it in Java.
Re: Clarification regarding finally blocks
Quote:
I'd like some clarification regarding when finally blocks will be executed....Can I just stick a finally block at the end of the function, and assume that it will be called no matter where the function returns from or what exceptions get thrown? Or does it have to follow a try block?
The finally block is only valid as part of a try block structure (optionally with catch statements). The finally block is guaranteed to execute no matter how the try block exits ie normally; via break, continue or return statements or through exceptions being thrown. If a catch statement catches an exception the catch statement code will execute before the finally code, but the finally block will still execute even if the catch statement returns or throws another exception.
Quote:
will the finally apply even if there's a return statement in the first catch, which is part of a different try/catch structure?
No, it will only be executed by the program leaving its own try/catch structure.
Re: Clarification regarding finally blocks
To clarify, if you have a 'finally' block in a method, it will always be run when the method is exited - whether by normal return, or via an exception. It is guaranteed to run before the method returns. 'finally' is an optional addition to a 'try' or 'try...catch' block. If you have more than one 'finally' block in a method, they will all be run.
What is not surrounded by uncertainty cannot be the truth...
R. Feynman
Re: Clarification regarding finally blocks
Quote:
Originally Posted by
keang
Quote:
will the finally apply even if there's a return statement in the first catch, which is part of a different try/catch structure?
No, it will only be executed by the program leaving its own try/catch structure.
Er, that should be 'yes' - the finally will run wherever it is in the method, and however many try...catches there are. If it compiles, it will be run.
One of the principal objects of theoretical research in any department of knowledge is to find the point of view from which the subject appears in its greatest simplicity...
J. W. Gibbs
Re: Clarification regarding finally blocks
The finally clause will always run except if there is a call to System.exit(0) in a try or a catch clause before the finally clause.
Also, if the finally clause throws any exceptions that is not caught inside the finally clause, it will not complete.
Re: Clarification regarding finally blocks
Quote:
Originally Posted by
Lindley
Code:
lock.lock();
try {}
catch {
return;
}
try {}
catch{}
catch{}
finally{
lock.unlock();
}
will the finally apply even if there's a return statement in the first catch, which is part of a different try/catch structure?
No, in this case the finally clause will not be executed. A finally clause belongs to the try/catch clause(s) just before itself.
Laitinen
Re: Clarification regarding finally blocks
Quote:
Quote:
Originally Posted by keang
Quote:will the finally apply even if there's a return statement in the first catch, which is part of a different try/catch structure?
No, it will only be executed by the program leaving its own try/catch structure.
Er, that should be 'yes' - the finally will run wherever it is in the method, and however many try...catches there are. If it compiles, it will be run.
No it's definitely a No. The example given has 2 try catch blocks, if the first try catch block returns from the method the finally cluase of the second try catch block won't execute.
Re: Clarification regarding finally blocks
Beg yer pardon everyone, my clarification wasn't clear at all and was totally incorrect :o That's what comes of posting in the middle of the night after too much wine... :sick:
But on a more practical note, you can ensure/guarantee a finally is run, whatever code comes before it in the method - in the example given, you wrap the method contents in a try block and put the finally containing the 'unlock()' at the end. This is might be what I thought I meant, I can't quite remember :rolleyes:
Code:
try {
lock.lock();
try {}
catch() {
return;
}
try {}
catch() {}
catch() {}
}
finally{
if (lock != null) {
lock.unlock();
}
}
This is one of the disadvantages of wine; it makes a man mistake words for thoughts...
Samuel Johnson
Re: Clarification regarding finally blocks
Quote:
Beg yer pardon everyone, my clarification wasn't clear at all and was totally incorrect That's what comes of posting in the middle of the night after too much wine...
The wonders of alcohol. Everything becomes crystal clear to those under the influence and utter gibberish to everyone else :)
Was it a nice bottle?
Re: Clarification regarding finally blocks
Quote:
Originally Posted by
keang
Was it a nice bottle?
We had a little more than one... :blush: And yes, it was very nice. Drink the best one first, because after about 1/2 a bottle, it doesn't really matter :rolleyes:
A bit disturbing to see how I can post syntactically correct posts with quotes & all, but containing arrogant rubbish :eek:
A timely reminder not to try and finish off any code after office lunchtime drinks or parties around Christmas.
I'd rather have a bottle in front of me than a frontal lobotomy...
Dorothy Parker
Re: Clarification regarding finally blocks
Quote:
We had a little more than one...
Maybe we should ask the mods to introduce a 'mail goggles' type checking system :D
Re: Clarification regarding finally blocks
Quote:
Originally Posted by
keang
Maybe we should ask the mods to introduce a 'mail goggles' type checking system :D
:D and no posts accepted after closing time... no, wait - that can't be right :p
I spent a lot of my money on booze, birds, and fast cars. The rest I just squandered...
George Best