After java throws an exception, whether the subsequent code continues to execute

        After java throws an exception, how are the following statements executed? Which ones will continue to execute and which ones will no longer be executed is the question to be explored in this chapter. In order to facilitate the majority of friends to solve the problem, first throw out the conclusion:

1. There is a try-catch statement block, and throw is in the catch statement block, then the subsequent code of the line of code that causes an exception (error) in the try statement block will not be executed and the code after the catch statement block will not be executed either (in case of to finally). (See Scenario 1 and Scenario 4)

2. If there is a try-catch statement block, and throw is in the try statement block, then the subsequent code of the line of code that causes an exception (error) in the try statement block will not be executed, but the code after the catch statement block will continue to execute. (see case 2)

3. If there is a try-catch statement block but no throw statement, then the subsequent code of the line of code that causes an exception (error) in the try statement block will not be executed, but the code after the catch statement block will continue to execute. (see case three)

4. If there is no try-catch statement block, only the throw statement block throws an exception, then the code behind throw will not be executed. (See Scenario 5)

        Let's study the various situations in which java throws exceptions.

Scenario 1:

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null没有length()方法,报空指针异常错误
            //下面两条赋值语句不会执行
            c = 1;  
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d);   //本条语句也不执行
    }
}

The result is as follows:

         Analysis: null does not have a length() method, so the line of code int b = a.length() will report a null pointer exception error, and then directly jump to the catch statement block to execute, and the value of c is still 0, indicating that c=1 is not executed, so the assignment is not successful. After the statement in the catch is executed, the program ends. System.out.println("The value of d is: " + d) This line of code is not executed. If you want this If the line of code is executed, it can be placed in the finally statement block, and the finally statement block will be executed after the catch statement block is executed.

Scenario 2:

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            if (a == null) {
                throw new RuntimeException("a的值不能是空");
            }
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c); //会执行
            //throw new RuntimeException(e);  //注释抛异常的函数
        } 
        System.out.println("d的值为:" + d); //会执行
    }
}

The result is as follows:

        Analysis: After an exception is thrown in the if, the subsequent assignment statement will not be executed, but directly jump out of the try statement block and enter the catch statement block, but the function that throws the exception in the statement block has been commented, so the program will continue to Execute below, thus printing out the initial value 0 of c and d.

Case three:

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length();  //null没有length()方法,报空指针异常错误
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            //throw new RuntimeException(e);   //该行注释掉
        }
        System.out.println("d的值为:" + d);   //会执行
    }
}

 The result is as follows:

         Analysis: After commenting out the throw new RuntimeException(e) line, no exception is thrown, and it will continue to go down, so the value of d can be printed out, but the printed values ​​of c and d are both initial values ​​0, assign The statement was not executed successfully.

Situation 4:

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            if (a == null) {
                throw new RuntimeException("a的值不能是空");
            }
            //下面两条赋值语句不会执行
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c); //会执行
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d); //不会执行
    }
}

The result is as follows:

        Analysis: Throw the exception in the if first, skip the execution of the assignment statement, directly execute the code in the catch, print out the initial value of c and receive an exception throw, and then the subsequent code will not be executed again. It is impossible to print out the value of d.

Scenario five:

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        if (a == null) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException("字符串a的值不能为空");  //throw语句不在try中
        }
        System.out.println("d的值为:" + d);   //该行代码不会执行
    }
}

The result is as follows:

         Analysis: throw new RuntimeException("The value of string a cannot be empty") customizes the thrown prompt information, which can be regarded as a return that returns the corresponding information. After the exception is thrown, the subsequent code will no longer Execution, so the value of d is not printed.

Situation 6 (normal situation where no exception is thrown):

public class ExceptionTest {
    public static void main(String[] args) {
        String a = "null";
        int c = 0, d = 0;
        try {
            int b = a.length();  //"null"有length()方法,正常执行
            //下面两条赋值语句会被执行
            c = 1;  
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d);   //本条语句也会被执行
    }
}

The result is as follows:

         Analysis: After changing null to "null", the length() method is valid. At this time, the line of code int b = a.length() does not report an error, and the subsequent two assignment statements are executed normally, so the program does not execute catch Therefore, the value of c will not be printed, and then the line of code System.out.println("the value of d: " + d) will be executed, and the value of d after reassignment will be printed out as 2.

Guess you like

Origin blog.csdn.net/liu__yuan/article/details/131284334