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
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
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.
Finally is ALWAYS executed, after the evaluation of the return statement.
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;
}
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
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.
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.