Does a finally block run even if you throw a new Exception?

JavaException Handling

Java Problem Overview


In this code will someVar be set even if the catch block is executed and the second Exception is thrown?

public void someFunction() throws Exception {
    try {
        //CODE HERE
    } catch (Exception e) {
        Log.e(TAG, "", e);
        throw new Exception(e);
    } finally {
        this.someVar= true;
    }
}

Java Solutions


Solution 1 - Java

Yes, the finally blocks always runs... except when:

  • The thread running the try-catch-finally block is killed or interrupted
  • You use System.exit(0);
  • The underlying VM is destroyed in some other way
  • The underlying hardware is unusable in some way

Additionally, if a method in your finally block throws an uncaught exception, then nothing after that will be executed (i.e. the exception will be thrown as it would in any other code). A very common case where this happens is java.sql.Connection.close().

As an aside, I am guessing that the code sample you have used is merely an example, but be careful of putting actual logic inside a finally block. The finally block is intended for resource clean-up (closing DB connections, releasing file handles etc), not for must-run logic. If it must-run do it before the try-catch block, away from something that could throw an exception, as your intention is almost certainly functionally the same.

Solution 2 - Java

Yes.

See the documentation:

> The finally block always executes when > the try block exits.

Exceptions:

> Note: If the JVM exits while the try > or catch code is being executed, then > the finally block may not execute. > Likewise, if the thread executing the > try or catch code is interrupted or > killed, the finally block may not > execute even though the application as > a whole continues.

Solution 3 - Java

Finally is always executed, no matter what your case is i.e

  • try-catch-finally block
  • throws

For unchecked exceptions, java does not mandate, error handling. this being the reason, if an unchecked exception occurs in finally block then and no handling is done for that, then code written below this point (where the error has occurred) will not be executed.

So I suggest to always handle all the exceptions may it be checked or unchecked. This way you can make sure that code block in finally is also executed no matter if unchecked exception also occurs. you have a place in sub-nest catch and Finally block to get your necessary work done.

Solution 4 - Java

Finally, block always executes.

public class ExceptionTest {

public static void someFunction(String input) throws Exception {
    try {
        if( input.equals("ABC") ) {
        	System.out.println("Matched");
        }
    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        System.out.println("Input Is "+input+" Finally Executed!!!");
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	try {
		System.out.println("********* Test with VALUE ********* ");
		someFunction("ABC");
		System.out.println("\r\n********* Test with NULL  ********* ");
		someFunction(null);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}

Java Try Catch Finally Block with Throw

Solution 5 - Java

The finally block always executes when the try block exits.unless you've System.exit(0) in your try or catch.

Solution 6 - Java

Yes. finally block executes always except the case you call System.exit() because it stops Java VM.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjaxView Question on Stackoverflow
Solution 1 - JavaGaryFView Answer on Stackoverflow
Solution 2 - JavafroadieView Answer on Stackoverflow
Solution 3 - JavayugView Answer on Stackoverflow
Solution 4 - JavaVasanth UmapathyView Answer on Stackoverflow
Solution 5 - JavaurmalpView Answer on Stackoverflow
Solution 6 - JavaVladimir IvanovView Answer on Stackoverflow