CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.
    Last edited by Lindley; December 4th, 2008 at 05:51 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Clarification regarding finally blocks

    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.

    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.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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
    Last edited by dlorde; December 4th, 2008 at 08:18 PM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Clarification regarding finally blocks

    Quote Originally Posted by keang View Post
    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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    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.

  6. #6
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Clarification regarding finally blocks

    Quote Originally Posted by Lindley View Post
    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

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Clarification regarding finally blocks

    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.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Clarification regarding finally blocks

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

    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
    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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Clarification regarding finally blocks

    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?

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Clarification regarding finally blocks

    Quote Originally Posted by keang View Post
    Was it a nice bottle?
    We had a little more than one... And yes, it was very nice. Drink the best one first, because after about 1/2 a bottle, it doesn't really matter

    A bit disturbing to see how I can post syntactically correct posts with quotes & all, but containing arrogant rubbish

    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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Clarification regarding finally blocks

    We had a little more than one...
    Maybe we should ask the mods to introduce a 'mail goggles' type checking system

  12. #12
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Clarification regarding finally blocks

    Quote Originally Posted by keang View Post
    Maybe we should ask the mods to introduce a 'mail goggles' type checking system
    and no posts accepted after closing time... no, wait - that can't be right

    I spent a lot of my money on booze, birds, and fast cars. The rest I just squandered...
    George Best
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured