Try-catch-finally in java

53,300

Solution 1

The only time a finally block will not be executed is when you call exit() before finally is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run.

EDIT: This is not entirely correct. See the comments below for additional information.

Solution 2

The finally block will always execute no matter if you return, or an exception is thrown within the try block.

See also section 14.19.2 Execution of try-catch-finally of the Java Language Specification

Solution 3

The finally block gets executed in all these cases. The point of executing finally block at the end is to allow you to release all the acquired resources.

Below is an example that shows how it works.

public class Hello {

    public static void hello(){
        try{
            System.out.println("hi");
            return;
            }catch(RuntimeException e){
        }finally{
            System.out.println("finally");

        }

    }

    public static void main(String[] args){
        hello();
    }
}

Solution 4

It gets executed even if you return inside the try block. I put one return statement inside try as well as one inside finally and the value returned was from finally, not from try.

public class HelloWorld{
   public static void main(String args[]){
      System.out.println(printSomething());
   }

   public static String printSomething(){
      try{
          return "tried";
      } finally{
          return "finally";
      }
   }
}

The output I got was "finally."

Share:
53,300

Related videos on Youtube

Nikhil
Author by

Nikhil

Updated on November 14, 2020

Comments

  • Nikhil
    Nikhil over 3 years

    In Java, will the finally block not get executed if we insert a return statement inside the try block of a try-catch-finally ?

    • Mark Peters
      Mark Peters over 12 years
      When you tried it, what happened?
    • home
      home over 12 years
      The finally block will always get executed. That's why it's called finally :-)
    • hansvb
      hansvb over 12 years
      Whatever you do, don't return from a finally block.
    • AlexR
      AlexR over 12 years
      @Thilo, +1. Once (many-many years ago) I wrote return from finally and then spent half a day looking for the problem it caused...
    • Raiyan
      Raiyan over 10 years
      @AlexR, just experienced what you had experienced "Once(many-many years ago)"
  • Mark Peters
    Mark Peters over 12 years
    Dunno if that's the only time. If System.exit() is fair game, isn't JNI code that segfaults? :-)
  • Stephen C
    Stephen C over 12 years
    There are at least 3 OTHER cases where the finally block is not executed: 1) if the try block or a catch block goes into an infinite loop, or blocks for ever, 2) if something (e.g. a JNI bug) causes the JVM to crash, or 3) if there is a machine outage (power failure, hardware error, etc).
  • Mark Peters
    Mark Peters over 12 years
    @Stephen: #1 isn't really valid. It didn't fail to execute, it just hasn't yet.
  • Stephen C
    Stephen C over 12 years
    Actually, if it is in a genuine infinite loop, it will NEVER execute unless the machine malfunctions.
  • sheidaei
    sheidaei about 11 years
    It is better to say that exit() wherever used in your Java program will make the program to exit, no matter if you use it in a finally block or anywhere else.
  • Andreas
    Andreas almost 10 years
    If an exception is thrown in a finally block, that implies execution reached the finally block. Perhaps the finally block will exit early due to the exception, but execution still gets there.
  • dylnmc
    dylnmc almost 10 years
    That is true; I'm not extremely familiar with finally blocks, but if there is an exception in the finally block, doesn't the finally block simply stop execution and default to the state that it was in before so - essentially - it is the same as the finally block not being executed in the first place? :P
  • Andreas
    Andreas almost 10 years
    exceptions in the finally block are thrown up the stack till something catches them. The only difference is that if there is an exception in a try block, and a catch block tries to throw it up the stack, if a finally block runs and throws its own exception, that new exception is what gets thrown up the stack. Effectively this masks the underlining exception that was caught. The finally block never rewinds execution to a previous state nor fails to fire (outside of system.exit() getting called)
  • Barranka
    Barranka almost 10 years
    If possible, can you include a link to the reference of this?
  • Yu Zhang
    Yu Zhang almost 8 years
    where is the output?
  • BJovke
    BJovke over 7 years
    Well if there's an exception in the "finally" block that means that "finally" block started executing. That's the whole point. What happens next in the "finally" block is not of importance for this question.