Java execution order to clarify the try-catch-finally return with a

Introduction: The band return and exceptions, performed between them try-catch-finally issue the order is left to a small doubt, they find out today

 

The first case: no abnormality

    //1.try-catch-finally都带有return
    public static int method_1(){
        int num = 1;
        try {            
            System.out.println("---try run---");
            return ++num;
        } catch (Exception e) {
            System.out.println("---catch run---");
            return --num;
        }finally {
            num = 10;
            System.out.println("---finally run---");
            // return num; 
        }         
    }

 

No abnormality, the program executing the content in the try finally executed last code block, the output result is:

 

1.1: You may be wondering: Why num value was modified in the finally block, the output is still 2?  

The reason is: when jdk went to try in return, will need to return a value into a temporary storage area to go, although finally the value of num has been modified and will not affect the results returned, the return is still a temporary storage area that

 

1.2: when a finally block the return Notes removed, the return value is Why 10?

 The reason: Whether it's a return, the final will finally execute the code in the try or catch, if carried out in return finally, quit this function.

 

The second case: abnormal

public static int method_2(){
        int num = 1;
        try {        
            num = num/0;
            System.out.println("---try run---");
            return ++num;
        } catch (Exception e) {
            System.out.println("---catch run---");
            return --num;
        }finally {
            num = 10;
            System.out.println("---finally run---");
            // return num; 
        }         
    }    

 Output:

In fact, very simple to understand: the block of code to try num = num / 0, is found to be wrong catch captured, the execution of a catch block, the last block of code is finally the same as in the first case, We need to return the value of the catch was placed in the stack, so finally the change does not affect the value of num actual return.

 

summary:

1.finally last block in the content code will be executed, either in or try the return catch in return

2.finally best not to carry out return operation is preferably carried out in a try-catch end of the function performed or

3.return value will be saved to the temporary storage area, even if the finally block return value to be modified, it will not affect the final value is returned.

 

Exercises:

Exercise one:

public static int method_3() {
        int num = 1; 
        try {
            num=1/0;
            return num;
        } catch (  Exception e) {
            System.out.println("catch run...");
            return num;
        } finally {
            ++num;
        }
     
    }

 

Exercise Two:

public static int method_4() {
        int num = 1;
        try {         
            return num;
        } catch (Exception e) {
            System.out.println("catch run...");        
        } finally {
            ++num;
        }
        return 20;
    }

Exercise Three:

    public static int method_5() {
        int num = 1;
        try {
            num = 1 / 0;
            return num;
        } catch (ArithmeticException e) {
            System.out.println("catch_1 run ...");
            return --num;
        } catch (Exception e) {
            System.out.println("catch_2 run ...");
            return ++num;
        } finally {
            ++num;
        }

    }

This problem is clearly the key num = num / 0 reported what is wrong

 

Other try-catch-finally exercises : https://blog.csdn.net/jinzhu_li/article/details/38560203

 

Guess you like

Origin www.cnblogs.com/zengcongcong/p/11315742.html