13

This is what I'm trying to do:

try {

    //code
} catch (Exception e) {

    return false;
} finally {

    //close resources
}

Will this work? Is it bad practice? Would it be better doing this:

boolean inserted = true;

try {

    //code
} catch (Exception e) {

    inserted = false;
} finally {

    //close resources
}

return inserted;
4
  • 6
    tried yourself? Commented Jul 31, 2013 at 20:21
  • dup alert! did you try searching? this has been asked several times over... Commented Jul 31, 2013 at 20:30
  • @devnull I researched but I was not sure if it was a good thing to do. Some topics said it was OK, while other people said it wasn't very good. Commented Jul 31, 2013 at 20:32
  • and are you certain of identical responses now? Commented Jul 31, 2013 at 20:37

4 Answers 4

24

Yes, it will. The only things that can prevent a finally block to execute (AFAIR) are System.exit(), and an infinite loop (and a JVM crash, of course).

Sign up to request clarification or add additional context in comments.

9 Comments

... and a power outage :) (blast, you ruined it with adding the JVM crash)
But is my first example a bad practice?
The interesting bit, overlooked by many, is that Thread#stop will not prevent the finally from executing.
@Thecoolguyacrossthestreet I would say the first is the simplest and what I would do.
@Thecoolguyacrossthestreet It would be a bad practice, if you had a return statement also in finally block because your catch block's return statement would be useless in that case.
|
7

The finally block is executed always, unconditionally, as the last thing the try-catch-finally block does. Even if you execute Thread#stop against it, the finally block will still execute, just as if a regular exception ocurred.

Not just that, if you return from finally, that return value will trample over the return from either try or catch.

BTW Your first example is not just fine, but preferred. In the second example the reader must chase around the assignments to the variable, which is a tedious job and lets bugs slip through very easily.

2 Comments

im not agreed with ..btw part ,i think is better to have only one return statement but that brings me to find about that here (where did the notion of one return only come from)[programmers.stackexchange.com/questions/118703/…
That's a nice writeup! (the first answer)
4

Both are approximately the same. However, be careful with the following case:

int i = 0;

try
{
    //code
}
catch(Exception e)
{
    return i;
}
finally
{
    i = 1;
}

0 is what will be returned.

2 Comments

Then I think my example is not a good practice, right?
@Thecoolguyacrossthestreet The best practice is to use clean code whenever possible.
0

I just wanted to add that it's described in the specs:

If the catch block completes abruptly for reason R, then the finally block is executed.

where of course

It can be seen, then, that a return statement always completes abruptly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.