9

If we give return statement like this in try, what will be the order of execution

try{
--- ----
-----
return a;
}

catch{
}
finally{
}

Here what will be order of execution if there is return in try. Please let me know

Nani
  • 363
  • 2
  • 4
  • 8

6 Answers6

4

http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2

finally always executes. If there is a return in try, the rest of try and catch don't execute, then finally executes (from innermost to outermost), then the function exits.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 4
    Not accurate. `finally` won't be executed if `System.exit()` is called or if the JVM crashes.. – Maroun Dec 12 '13 at 08:34
  • 9
    Yeah, it won't execute if you unplug the computer from the wall either. It's the same kind of situation, not really necessary to qualify. – Mumbleskates Dec 12 '13 at 08:43
  • 1
    @Widdershins Or if a dinosaur will eat your machine. But invoking `System.exit()` is a case you should take care of. (I agree about JVM crashing..) – Maroun Dec 12 '13 at 09:00
  • @Maroun Maroun Yeah, though honestly with the way `System.exit()` performs, it is essentially an internally-induced system-crash. Probably the dirtiest way to ever close any Java application, it seems to make no guarantees about how much could get done after it's called unless you have a shutdown hook that is very, very careful about `join()`ing your must-complete tasks. I have an application that has yet to dump many megabytes to file when `System.exit(0)` is called and it is either difficult or impossible to get it to finish. Honestly it would be better to not call `System.exit()` at all. – Mumbleskates Dec 12 '13 at 18:21
  • 2
    @Maroun Maroun Looks like shutdown hooks `join`ing must-complete tasks are considered as blocking-trapped and will be terminated; you have to make sure your shutdown hooks actually do the work themselves. So I figured that out. I still think `exit()` is a bad way to shut down the machine and should be avoided if you have any better way to handle it. – Mumbleskates Dec 12 '13 at 18:38
1

Finally is ALWAYS executed, after the evaluation of the return statement.

gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46
  • 1
    Well, the expression in the return statement will still be evaluated before the `finally` clause. – Niklas R Dec 12 '13 at 08:39
1

If there is a return in try, then control will go to finally block , execute the code present and then exit. So during this if in finally block there is any change to the any of the variable returned in try, and if that same variable is returned in finally then latest will be returned.

try {
             i = 11;
             return i;
            } catch (Exception e) {
                // TODO: handle exception
            } finally{

                i = 12;
                return i; --> This will be returned
            }
        //return i;


     }

But if there is only modification , no retrun in finally, the value returned in try will be the final value.

 try {
             i = 11; --> this will be returned
             return i;
            } catch (Exception e) {
                // TODO: handle exception
            } finally{

                i = 12; -- this will be executed

            }
        //return i;


     }
Vineet Kasat
  • 994
  • 7
  • 14
0

Whatever might be the case finally will always execute. Incase of sucessful execution of try it will not execute catch block. If try blocks throws exception then catch block will execute

Sachin
  • 3,424
  • 3
  • 21
  • 42
0

Normally order execution order of try-catch-finally is first try, then if exception trows and caught will execute the catch. If exception caught or not finally will always execute.

If return in your try, execution in try will stop there and will execute finally. if exception throws and caught before that return normal execution order will follows.

Let's run following code

public static void main(String[] args) {
    String[] arr=getInfo();
    for(String i:arr){
        System.out.println(i);
    }
}

public static String[] getInfo(){
    String[] arr=new String[3];
  try {
      arr[0]="try";
      return arr;
  }catch (Exception e){
      arr[1]="catch";
      return arr;
  }finally {
      arr[2]="finally";
      return arr;
  }
}

Out put

try // return in try
null
finally // returning value in finally.  

Now this out put explain the every thing you want. finally runs while there is a return in try.

If there a System.exit(0) in your try, finally is not going to execute.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • You are specifying the order when you set the values of the array so naturally, that would be the order – Will Jan 29 '19 at 21:18
-1

in try-catch handling, when you return something from a try block, it will goes out of the scope of try-catch-finally.. because catch block only accept what is thrown by try.. not the thing that is returned and now when you have returned from the try block it wont reach the try end and finally wont be executed, it will go out try-catch-finally block, back to your code.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
Yogesh Kumar
  • 682
  • 1
  • 10
  • 29