After Java throws an exception, will subsequent code still be executed?

After java throws an exception, how are the subsequent statements executed? Which will continue to be implemented and which will no longer be implemented is the question to be explored in this chapter. In order to facilitate the majority of friends to solve the problem, let me 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 the exception (error report) in the try statement block will not be executed, and the code after the catch statement block will not be executed (in case of Except 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 the exception (error report) in the try statement block will not be executed, but the code after the catch statement block will continue to execute. (See situation 2)

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

4. There is no try-catch statement block, and there is only a throw statement block that throws an exception, then the code after the throw will not be executed. (See situation 5)

Below we will 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 has no length() method and reports a null pointer exception error
            //The following two assignment statements will not be executed.
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("The value of c is:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("The value of d is:" + d); //This statement will not be executed either
    }
}

The result is as follows:

Picture

Analysis: null does not have a length() method, so this line of code int b = a.length() will report a null pointer exception error, and then jump directly to the catch statement block for execution, printing out c The value is still 0, indicating that c=1 has not been executed and therefore the assignment has not been successful. After executing the statement in catch, the program ends, System.out.println("The value of d is:" + d) This line of code will not be executed. If you want this line of code to be executed, you can place it in the finally statement block. After the catch statement block is executed, the finally statement block will be 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("The value of a cannot be empty");
            }
            //The following two assignment statements will not be executed.
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("The value of c is:" + c); //Will be executed
            //throw new RuntimeException(e); //Comment the function that throws the exception
        }
        System.out.println("The value of d is:" + d); //Will be executed
    }
}

The result is as follows:

Picture

Analysis: After an exception is thrown in the if, the subsequent assignment statement is no longer executed. Instead, the try statement block is jumped directly and entered into the catch statement block. However, the function that throws the exception in the statement block has been commented, so the program will continue. Execute below, thereby printing out the initial values 0 of c and d.

Scenario 3
public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length(); //null has no length() method and reports a null pointer exception error
            //The following two assignment statements will not be executed.
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("The value of c is:" + c);
            //throw new RuntimeException(e); //Comment out this line
        }
        System.out.println("The value of d is:" + d); //Will be executed
    }
}

The result is as follows:

Picture

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

Scenario 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("The value of a cannot be empty");
            }
            //The following two assignment statements will not be executed.
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("The value of c is:" + c); //Will be executed
            throw new RuntimeException(e);
        }
        System.out.println("The value of d is:" + d); //Will not be executed
    }
}

The result is as follows:

Picture

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

Scenario 5
public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        if (a == null) {
            System.out.println("The value of c is:" + c);
            throw new RuntimeException("The value of string a cannot be empty"); //throw statement is not in try
        }
        System.out.println("The value of d is:" + d); //This line of code will not be executed
    }
}

The result is as follows:

Picture

Analysis: throw new RuntimeException("The value of string a cannot be empty") customizes the thrown prompt information, which can be regarded as a return and returns the corresponding information. After throwing The code following the exception will not be executed again, so the value of d will not be printed.

Scenario 6 (normal situation without exception thrown)
public class ExceptionTest {
    public static void main(String[] args) {
        String a = "null";
        int c = 0, d = 0;
        try {
            int b = a.length(); //"null" has length() method and executes normally
            //The following two assignment statements will be executed
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("The value of c is:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("The value of d is:" + d); //This statement will also be executed
    }
}

The result is as follows:

Picture

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