Home » Java » Try catch finally return value problem

Try catch finally return value problem


1, regardless of the wood exception, the code in the finally block will execute;

2, when there is a return in try and catch, you wait until finally finishes and then returns;

3, the finally is executed in the back of the return expression after operation (at this time and did not return after computing the value, but the first to return to the values stored in the finally code, the tube like, the return value will not change, is still before the stored value), so the function return value is in execution before the finally
4, finally is best not to include return, otherwise the program will exit in advance, and the return value is not the return value catch stored in try or.

, as shown in the code, try, catch, and finally return the "Try", "Catch", "Finally",

, respectively
, public, static, void, main (String[], args) {
Auto-generated method stub / / TODO
ExceptionTest, e=new, ExceptionTest ();
System.out.println (e.getExc ());
Public, String, getExc () {
Return "Try"";
} catch (Exception, e) {
Return "Catch"";
In order to perform finally / / try or catch but the return value will output at finally after execution;
//finally will execute, and finally return to return;
System.out.println ("Finally statement must execute");
The return in //try catch has been generated, but will not be returned until the finally executes;
Return "Finally"";
If the finally return / / catch try will cover return and exit;

running results

The Finally statement must execute


So you'll overwrite the try catch return;

uses int i=1000/0 to produce arithmetic exception tests with the same result;