I am learning Exception handling in java. I just want to know, does the finally() block always get executed in Java Exception Handling?

this code in java, can I be absolutely sure that the finally block always executes, no matter what something() is?

try {
    System.out.println("I am in try block");
} catch(Exception ex){
    ex.printStackTrace();
} finally {
    System.out.println("I am in finally block");
}

can anyone explain that how Java Exception Handling works?

thank you in advance.
 

error-handling java return try-catch-finally 

Nov 17

3 Answers

Yes, finally() will be called after the execution of the try or catch code blocks.

The only times finally won't be called are:

  • If you invoke System.exit()
try {
    System.out.println("I am in try block");
    System.exit(1);
} catch(Exception ex){
    ex.printStackTrace();
} finally {
    System.out.println("I am in finally block");
}
  • If you invoke Runtime.getRuntime().halt(exitStatus)
  • If the JVM crashes first
  • If the JVM reaches an infinite loop (or some other non-interruptible, non-terminating statement) in the try or catch() block
  • If the OS forcibly terminates the JVM process; e.g., kill -9 <pid> on UNIX
  • If the host system dies; e.g., power failure, hardware error, OS panic, et cetera
  • If the finally() block is going to be executed by a daemon thread and all other non-daemon threads exit before finally is called

answered Dec 27


Yes in java exception handling, the finally() block will be always executed even after a return; statement in a method.

If we call the System.exit() the method explicitly in the finally block then only it will not be executed.

There are few situations where the finally() will never been executed like,

  • JVM(Java Virtual Machine) crash
  • Power failure
  • Software crash

Other than these conditions, the finally() block will be always executed in java exception handling.

let's try one example for java exception handling with a return statement.

Example:

public class FinallyBlockAfterReturnTest {
    public static void main(String[] args) {
        System.out.println(count());
    }
    public static int count() {
        try {
            return 1;
        } catch(Exception e) {
            return 2;
        } finally {
            System.out.println("Finally block will execute even after a return statement in a method");
        }
    }
}

Output:

Finally block will always excute even after a return statement in a method
1

answered Dec 27


Yes in java exception handling, the finally() block is always get executed unless there is an abnormal program termination either resulting from a JVM crash or from a call to System.exit().

Example 1

public class FinallyBlock {
   public static void main(String args[]){
      try {
         int a=10,b=30;
         int c = b/a;
         System.out.println(c);
      } catch(ArithmeticException ae){
         System.out.println(ae);
      } finally {
         System.out.println("finally block is always executed");
      }
   }
}

In the above example, the finally() block always gets executed if the exception has occurred or not.

Output

3
finally block is always executed

Example 2

public class FinallyBlock {
   public static void main(String args[]) {
      try {
         System.out.println("I am in try block");
         System.exit(1);
      } catch(Exception ex){
         ex.printStackTrace();
      } finally {
         System.out.println("I am in finally block");
      }
   }
}

In the above example, the finally() block will not execute due to the System.exit(1) condition in the try block.

Output

I am in try block

Summary

In java exception handling finally() block is always get executed whether the exception has occurred or not.

If an exception occurs like closing a file or DB connection, then the finally() block is used to clean up the code.

There are few situations where the finally() will never be executed like,

  • JVM(Java Virtual Machine) crash
  • Power failure
  • Software crash
  • JVM(Java Virtual Machine) runs out of memory
  • Deadlock condition in our try block.

answered Dec 27


Login and Submit Your Answer
Browse other questions tagged  error-handling  java  return  try-catch-finally or ask your own question.