What exactly does a finally
block in exception handling perform?
If you return a value in your try
or catch
block as well as in the finally
block, keep in mind that the finally
block's return value is what you'll end up with (the last block executed). That means if you try
some code that does NOT throw an Exception
and is supposed to return a value, but your finally
block is also supposed to return a value, the finally
block's value is what will actually be returned. This SO thread talks about that very point. I don't believe returning a value inside a try
or catch
is usually necessary or the best idea. Also note that System.exit(0)
kills the JVM and thus halts execution before anything else runs, which might render your finally
block unexecuted.