try catch finally execution sequence

We all know that
the try {
} chatch () {
} a finally {
}
is the java exception handling most common form of the method, the interview often examinations to this knowledge, I have read countless times, but always easy to forget Maybe write will be memorable on us.
suppose try block a return statement, then the catch, and finally code will execute it? If you will perform, then order what is it? I wrote a test class were tested these problems:
test 1:
class Test22
{
public static String test () {
the try {
// the throw new new Exception ();
return "return";
} the catch (Exception E) {
System.out.println ( "the catch");
} {the finally
return "the finally";
}
}
public static void main (String [] args)
{
System.out.println (Test ());
}
}
The output is: finally
This shows that finally executed first, in fact, I this can also understand the meaning of return to understand this order, return means out of this statement block, jumped out naturally can not come back inside the implementation of the statement, while finally is to be performed, so before executing the finally let out of it.

test two:
class Test22
{
public static String the test () {
the try {
the throw new new Exception ();

} the catch (Exception E) {
System.out.println ( "the catch");
} {the finally
return "the finally";
}
}
public static void main (String [] args)
{
System.out.println (Test ());
}
}
the output:
the catch
the finally
this is the usual sequence of the most common, try the forces an exception, catch is executed, and then is finally in a statement.
test three:
class Test22
{
public static String the test () {
the try {
Exception new new the throw ();
} the catch (Exception E) {
return "the catch";
} the finally {
return "the finally";
}
}
public static void main (String [] args)
{
System.out.println (Test ());
}
}
the output:
a finally
there is a catch in return and try block has return is the same situation.


this time I should have a more thorough understanding, and hope not to forget.

Guess you like

Origin www.cnblogs.com/yongdao/p/11506129.html