Will java try catch continue execution after exception?

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 2)

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 three)

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 4)

4. There is a try-catch statement block, but there is no throw statement. If the exception object does not match the exception type declared in catch, the program will interrupt. (See situation 5)

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

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
    }
}

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 jump directly to the catch statement block for execution, and the value of c printed out is still 0, indicating that c=1 is not executed, so the assignment is not successful. After executing the statement in catch, the program ends. System.out.println (“The value of d is: ” + d) will not be executed. If you want this If the line of code is executed, it can be placed 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);
        }
        System.out.println("The value of d is: " + d); //Will not be executed
    }
}

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 3

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
    }
}

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 4

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
    }
}

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, but the printed values of c and d are both the initial value 0, assignment The statement was not executed successfully.

Scenario 5

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 (IndexOutOfRangeException 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
    }
}

Analysis: The exception type does not match, the program is interrupted, and the values of c and d will not be printed.

Scenario 6

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
    }
}

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 the exception is thrown, the subsequent code will not executed, so the value of d is not printed.

Scenario 7 (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
    }
}

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 following two assignment statements are executed normally, so the program does not execute catch. statement in, so the value of c will not be printed, and then execute System.out.println(“The value of d is: ” + d), this line of code will print out the value of d after reassignment is 2.

catch{}commonly used exception types

Exception Description
SystemException Basic class for other user-handled exceptions
ArgumentException The parameter of the method is illegal
ArgumentNullException A null parameter is passed to the method and the method cannot accept the parameter
ArgumentOutOfRangeException The parameter value is out of range
ArithmeticException Arithmetic overflow or underflow occurs
ArrayTypeMismatchException Attempting to store an object of the wrong type in the array
BadImageFormatException The format of the graphic is wrong
DivideByZeroException Divide by zero exception
DllNotFoundException The referenced DLL cannot be found
FormatException Parameter format error
IndexOutOfRangeException Array index out of range
InvalidCastException Using invalid class
InvalidOperationException The calling time of the method is wrong
MethodAccessException Attempt to access Siyou or protected method
MissingMemberException Accessing an invalid version of the DLL
NotFiniteNumberException The object is not a valid one Member
NotSupportedException The method called is not implemented in the class
NullReferenceException Attempted to use an unallocated reference
OutOfMemoryException Insufficient memory space
PlatformNotSupportedException This error is thrown when the platform does not support a specific attribute
StackOverflowException Stack Overflow