Backend Development 9 min read

Understanding Java Exception Flow: Which Statements Execute After a Throw

This article explains how Java's try‑catch and throw statements affect the execution of subsequent code, detailing six different scenarios to show which lines run, which are skipped, and how finally blocks can change the outcome.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Java Exception Flow: Which Statements Execute After a Throw

Java's exception handling determines which statements are executed after a throw . This article examines six scenarios involving try‑catch blocks, throws inside or outside the catch clause, and normal execution, illustrating which lines run and which are skipped.

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, throws NullPointerException
            // the following assignments will not execute
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d); // this line also does not execute
    }
}

Analysis: The call to a.length() throws a NullPointerException, causing control to jump to the catch block; the variable c remains 0 because the assignment never ran, and the statement after the catch block is not 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的值不能是空");
            }
            // the following assignments will not execute
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c); // will execute
            // throw new RuntimeException(e); // commented out
        }
        System.out.println("d的值为:" + d); // will execute
    }
}

Analysis: The exception is thrown inside the if statement, so the assignments are skipped and control moves to the catch block; because the re‑throw is commented out, execution continues after the catch , printing the initial values of c and d (both 0).

Scenario 3

public class ExceptionTest {
    public static void main(String[] args) {
        String a = null;
        int c = 0, d = 0;
        try {
            int b = a.length(); // NullPointerException
            // the following assignments will not execute
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            // throw new RuntimeException(e); // commented out
        }
        System.out.println("d的值为:" + d); // will execute
    }
}

Analysis: By commenting out the re‑throw, the program continues after the catch block; d is printed, but both c and d retain their initial values (0) because the assignments never ran.

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("a的值不能是空");
            }
            // the following assignments will not execute
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c); // will execute
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d); // will not execute
    }
}

Analysis: The exception thrown in the if block skips the assignments, the catch prints the initial c value (0) and re‑throws a new exception, so the code after the catch (printing d ) never runs.

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("c的值为:" + c);
            throw new RuntimeException("字符串a的值不能为空"); // throw not inside try
        }
        System.out.println("d的值为:" + d); // this line will not execute
    }
}

Analysis: The explicit throw is outside any try block, so after printing c the program terminates; the subsequent statement printing d is never reached.

Scenario 6 (No 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(), no exception
            // following assignments will be executed
            c = 1;
            d = 2;
        } catch (Exception e) {
            System.out.println("c的值为:" + c);
            throw new RuntimeException(e);
        }
        System.out.println("d的值为:" + d); // this line will be executed
    }
}

Analysis: Because a is a non‑null string, a.length() succeeds, the assignments run, the catch block is skipped, and the final print statement outputs d = 2.

BackendJavaException Handlingprogrammingtry-catch
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.