How does return in try, catch and finally affect the word order of code execution

1. If there is no return statement in try, catch and finally, the normal execution sequence is as follows:

After the normal execution of the try code block, continue to execute the finally code block, and finally execute the statement block after finally;

If an exception occurs in the execution of the try code block, the catch block will be executed. After the catch block is executed, the finally code block will be executed, and finally the statement block after finally will be executed.

public class Main {
static String str="";
public static void main(String[] args) {
//Scanner scanner = new Scanner(system.in);在线笔试代码用
test(0);
System.out.println(str);

    str="";
    test(1);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(str);
}

private static void test(int i){
    try{
        if(i==1)
            throw new Exception();
        else {
            str+="111-";
            //return;
        }
    }catch (Exception e){
        str+="222-";
        //return;
    }finally {
        str+="333-";
        //return;
    }
    str+="444";//Unreacheable statement
}

}

<code class="language-java">111-333-444
222-333-444

2. (1) if there is a return in finally, no matter whether there is a return in try or catch, the code behind the finally statement block will not be executed, and the compiler will prompt an error: unreachable statement.

public class Main {
static String str="";
public static void main(String[] args) {
//Scanner scanner = new Scanner(system.in);在线笔试代码用
test(0);
System.out.println(str);

    str="";
    test(1);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(str);
}

private static void test(int i){
    try{
        if(i==1)
            throw new Exception();
        else {
            str+="111-";
            //return;
        }
    }catch (Exception e){
        str+="222-";
        //return;
    }finally {
        str+="333-";
        return;
    }
    str+="444";//Unreacheable statement
}

}

(2) If there is a return in both try and catch blocks, no matter whether there is a return in finally or not, the code behind the finally statement block will not be executed, and the compiler will prompt an error: unreachable statement.

public class Main {
static String str="";
public static void main(String[] args) {
//Scanner scanner = new Scanner(system.in);在线笔试代码用
test(0);
System.out.println(str);

    str="";
    test(1);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(str);
}

private static void test(int i){
    try{
        if(i==1)
            throw new Exception();
        else {
            str+="111-";
            return;
        }
    }catch (Exception e){
        str+="222-";
        return;
    }finally {
        str+="333-";
        //return;
    }
    str+="444";//Unreacheable statement
}

}

3. No matter whether there is a return in try, catch or finally, the combination that will be executed is try finally or try catch finally. In the following code, there are three marked line numbers. No matter whether they are used or not, they will not affect the final output result.

public class Main {
static String str="";
public static void main(String[] args) {
//Scanner scanner = new Scanner(system.in);在线笔试代码用
test(0);
System.out.println(str);

    str="";
    test(1);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(str);
}

private static void test(int i){
    try{
        if(i==1)
            throw new Exception();
        else {
            str+="111-";
            return;//1
        }
    }catch (Exception e){
        str+="222-";
        return;//2
    }finally {
        str+="333-";
        return;//3
    }
    //str+="444";//Unreacheable statement
}

}

/**
 * 测试问题:在返回值方面:finally里面的return值会覆盖try,catch里面的return值
 * @param num
 * @return
 */
static int getNum(int num){
    try{
        int result=2/num;
        return result;
    }catch(Exception e){
        return 0;
    }finally {
        if(num==0)
            return -1;
        if(num==1)
            return 1;
    }
}

Output results:

<code class="language-html">-1
1
1
0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>