Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of execution in try,catch and finally [duplicate]

Tags:

java

exception

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

like image 323
Nani Avatar asked Dec 12 '13 08:12

Nani


People also ask

Which executes first catch or finally?

finally defines a block of code we use along with the try keyword. It defines code that's always run after the try and any catch block, before the method is completed. The finally block executes regardless of whether an exception is thrown or caught.

What is the order of execution of finally block when there are exceptions in TRY block?

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

Does finally execute after throw in catch?

A finally block always executes, regardless of whether an exception is thrown. The following code example uses a try / catch block to catch an ArgumentOutOfRangeException.

What is the correct implementation of the try catch?

Java try block It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception. Java try block must be followed by either catch or finally block.


1 Answers

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.

like image 187
Amadan Avatar answered Sep 28 '22 01:09

Amadan