在只有try-catch语句中,如果catch块中出现了return语句或者抛出了异常,那么catch之后的语句是执行不到的;但是如果将代码放入finally中,即使catch中出现了return语句或者抛出了异常,finally中的代码仍然是可以执行到的。看下面的程序:
public class Test {public static void main (string[] args) {//TODO auto-generated Method StubSystem.out.println (Test ());} public static int test () {int b = 23;try {System.out.println ("yes"), return B + =,} catch (Exception e) {System.out.print ln ("error:" + e);} System.out.println ("Next"); return b;}}
Because there is no exception in the try block and there is a return statement, the subsequent output program is not executed. However, if you put the output program in the Finally, it will be executed to.
try {System.out.println ("yes"), return B + =,} catch (Exception e) {System.out.println ("error:" + e);} Finally{system.out.println ("finally");}
Look at the first paragraph of the program, and finally there is a return B statement. If this statement is missing, the program will error, prompting the test () method to return a value of type int. One might find that a return statement already appears in the Try statement, and why the error is still being made. This is because the code that is placed in the try block is likely to have an exception, and if an exception occurs, the system will no longer execute code that is not executed in the try block. So there is a situation where the program has not executed the return statement, because the exception to execute the catch statement. In this case, the program skips the return statement, so a return must be added to the back.
Also, if we put the return statement in the finally block, a "finally does not complete normally" warning appears. The reasons may be as follows:
1. The finally block executes regardless of whether there is a return statement in the try block or catch block.
2. The return statement in the finally block overwrites the previous return statement (the try block, the return statement in the catch block), so if there is a return statement in the finally block, the Eclipse compiler will report a warning "finally block Does not complete normally ".
3. If the finally block contains a return statement, even if the previous catch block re-throws an exception, the statement calling the method does not get the exception that the catch block re-throws, but instead gets the return value of the finally block and does not catch the exception.
Let's look at the problem of finally and return. If we add some code to finally in the first paragraph of the program, as follows:
Package Com.imooc1;public class Test {public static void main (string[] args) {//TODO auto-generated method Stubsystem.out . println (Test ());} public static int test () {int b = 23;try {System.out.println ("yes"), return B + =,} catch (Exception e) {System.out.print ln ("error:" + e);} finally {if (b >) {System.out.println ("b>25:" + b);} System.out.println ("finally");} return b;}}
How does the program work? This is primarily used to test which return and finally statements are executed first. In the Try statement, the b=111 is returned; In the finally statement, whether it is greater than 25, if large, the output is large. The final result is as follows:
Yes
b>25:111
Finally
111
This indicates that the finally statement was executed after the return statement of the try and the return returned.
If you change the finally statement to this (there will be a warning that appears above):
Package Com.imooc1;public class Test {public static void main (string[] args) {//TODO auto-generated method Stubsystem.out . println (Test ());} public static int test () {int b = 23;try {System.out.println ("yes"), return B + =,} catch (Exception e) {System.out.print ln ("error:" + e);} finally {if (b >) {System.out.println ("b>25:" + b);} System.out.println ("finally"); return 100;} return b;}}
The result becomes:
Yes
b>25:111
Finally
100
This means that the return statement in the finally block overwrites the return in the try block!
If you turn the statement in finally into this:
Package Com.imooc1;public class Test {public static void main (string[] args) {//TODO auto-generated method Stubsystem.out . println (Test ());} public static int test () {int b = 23;try {System.out.println ("yes"), return B + =,} catch (Exception e) {System.out.print ln ("error:" + e);} finally {if (b >) {System.out.println ("b>25:" + b);} System.out.println ("finally"); b=100;} return b;}}
That is, change the value of B in finally. So is the return result the original B-value, or the changed B-value? The results are as follows:
Yes
b>25:111
Finally
111
Illustrates a problem where the original return value does not change if you do not have a return statement overlay in the finally statement, whether or not you have changed the variable you want to return. However, if the program does not execute a return in the try because of an exception, the change is valid. The test code is as follows:
Package Com.imooc1;public class Test {public static void main (string[] args) {//TODO auto-generated method Stubsystem.out . println (Test ());} public static int test () {int b = 23;int A = 0;try {System.out.println ("b/a" +b/a); return B + = n;} catch (Arithmeticexcept Ion e) {e.printstacktrace ();} finally {if (b >) {System.out.println ("b>25:" + b);} System.out.println ("finally"); b=100;} return b;}}
The results of the program run as follows:
Java.lang.ArithmeticException:/By zero
At Com.imooc1.Test.test (test.java:14)
At Com.imooc1.Test.main (test.java:7)
Finally
100
A little understanding of try-catch-finally in Java