11.3 throw, throws & Overriding Exception Rules

Key Takeaways

  • A throw statement raises an exception from an expression. A throws clause in a declaration lists the types of exceptions that the method or constructor can propagate.

  • Any statement written immediately after an unconditional throw in the same block fails compilation as unreachable, while throw null compiles cleanly and throws NullPointerException at runtime

  • Declaring a checked exception in a method's throws clause is legal even if the body never throws it, and imposes the Catch-or-Specify rule on callers including main

  • An override can omit or narrow checked exceptions, subject to every method it overrides. The checked-exception rule places no restriction on unchecked declarations.

  • An explicit subclass constructor must account for checked exceptions from its constructor invocation. Its throws clause can name the checked type or a supertype.

Last updated: November 2026

throw, throws & Overriding Exception Rules

Independent prep covering throwing, declaring, and overriding exceptions in Official Domain 8 (Handling Exceptions). Two keywords separated by a single letter—throw and throws—cause constant confusion under exam time pressure, and the contrasting checked-exception rules for overriding methods (same or narrower) versus subclass constructors (same or broader) are among the highest-yield topics on 1Z0-808.

throw vs. throws: Syntax & Placement

Every throw and throws question starts with verifying that each keyword appears in its legal grammatical position with the right operand:

Featurethrowthrows
Where usedInside a method, constructor, or initializer bodyIn a method or constructor signature header
OperandAn expression of throwable type, or the null typeOne or more throwable types, including bounded type variables where legal
EffectCompletes abruptly after evaluating the operand, triggering enclosing catch/finally processingAdvertises checked/unchecked exceptions that callers may receive
Common TrapWriting throw IOException; (missing new / object) or placing throw in a headerWriting throws new IOException() or placing throws inside a method body
import java.io.IOException;

public class ThrowVsThrows {
    // Correct: 'throws' in header with class name; 'throw' in body with instance
    void validate(int size) throws IOException {
        if (size < 0) {
            throw new IOException("Negative size: " + size);
        }
    }

    public static void main(String[] args) {
        ThrowVsThrows validator = new ThrowVsThrows();
        try {
            validator.validate(-1);
            System.out.println("Accepted");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    // void badHeader() throw IOException {}          // DOES NOT COMPILE: 'throw' in signature
    // void badBody() { throws new IOException(); }   // DOES NOT COMPILE: 'throws' in body
}

Calling validate(-1) throws the checked exception. The handler prints Negative size: -1, and the Accepted line is skipped. With validate(3), the call returns normally and the program prints Accepted.

Unreachable Code After throw & the throw null Trap

Because an unconditional throw transfers control out of the current block immediately—just like return—any statement directly following it in the same block fails compilation with unreachable statement:

void failFast() {
    throw new IllegalStateException("abort");
    // System.out.println("Done"); // DOES NOT COMPILE: unreachable statement
}

Watch one notorious runtime twist: throw null; compiles cleanly! Because the null literal is compatible with Throwable, javac accepts throw null; (and RuntimeException e = null; throw e;). At runtime, attempting to throw a null reference throws a NullPointerException instead.


Declaring Exceptions with throws & Caller Obligations

Under the Catch-or-Specify rule, whenever a method calls another method whose signature declares a checked exception, the caller has two choices:

  1. Catch it inside a matching try-catch block.
  2. Declare it by adding that checked exception (or a superclass such as Exception or Throwable) to the caller's own throws clause—including on main.
import java.io.IOException;

public class CallerOptions {
    static void connect() throws IOException {
        // Notice: body does NOT actually throw IOException, yet declaration compiles!
    }

    public static void main(String[] args) throws IOException {
        connect(); // Valid because main declares throws IOException
    }
}

Look closely at connect(): a method can declare a checked exception in throws even when its body never throws that exception. Callers must still handle or declare IOException! This is the exact opposite of catch (IOException e), which fails compilation if the try block cannot throw IOException. Why the asymmetry? A throws clause can express an interface contract even if one concrete implementation never throws the declared type.

By contrast, adding an unchecked exception such as throws IllegalArgumentException or throws Error to a method header is completely legal for documentation, and imposes zero compile-time obligation on callers.

Rethrowing Exceptions

For a catch parameter that is final or effectively final, Java 8 can infer a narrower rethrow type from the try block and earlier handlers. For example, if the try’s only possible checked exception is IOException, catch (Exception e) { throw e; } can work in a method declaring throws IOException, as long as e is not reassigned. Propagating a checked type still requires a covering declaration or an enclosing handler.


Method Overriding Exception Rules

When a subclass overrides an inherited instance method—or implements an interface method—the compiler enforces strict compatibility on checked exceptions so callers programming against the superclass or interface reference are never surprised by an unhandled checked exception.

Important

An overriding method may declare the same checked exceptions, narrower checked subclasses, or fewer/no checked exceptions. It may never declare a new or broader checked exception. The checked-exception restriction does not apply to unchecked types.

Let's trace valid and invalid overrides in a hierarchy where FileNotFoundException extends IOException:

import java.io.FileNotFoundException;
import java.io.IOException;

class Reader {
    public void read() throws IOException {}
    public void close() {}
}

class FastReader extends Reader {
    @Override
    public void read() throws FileNotFoundException {} // VALID: narrower checked exception

    @Override
    public void close() throws IllegalStateException {} // VALID: RuntimeException is unrestricted
}

class SilentReader extends Reader {
    @Override
    public void read() {} // VALID: omitting checked exceptions is allowed
}

class BrokenReader extends Reader {
    // @Override
    // public void read() throws Exception {} // DOES NOT COMPILE: Exception is broader than IOException

    // @Override
    // public void close() throws IOException {} // DOES NOT COMPILE: Reader.close() throws no checked exception
}

Notice BrokenReader.close(): because Reader.close() declares no checked exceptions, callers invoking Reader r = new BrokenReader(); r.close(); are not required to write a try-catch. Allowing BrokenReader.close() to add throws IOException would break type safety, so javac rejects it. The exact same rule applies when implementing interfaces such as Runnable (public void run() declares no checked exceptions, so an implementation cannot add throws IOException).


Constructor Exception Rules (The Opposite Direction!)

Constructors are never inherited and therefore never override superclass constructors. In an ordinary subclass, a constructor invokes a superclass constructor or delegates to a same-class constructor that ultimately does so. Java SE 8 requires an explicit this(...) or super(...) invocation to be the first statement.

Because super(...) must be the first statement, a subclass constructor cannot wrap super(...) inside a try-catch block! Therefore, if the invoked superclass constructor declares a checked exception, the subclass constructor is forced to declare the same or a broader checked exception:

Member KindChecked Exceptions Relative to SuperclassUnchecked Exceptions
Overriding MethodSame, narrower, or noneUnrestricted
Subclass ConstructorSame or broader (cannot narrow or drop if super(...) throws it)Unrestricted
import java.io.FileNotFoundException;
import java.io.IOException;

class Device {
    Device() throws FileNotFoundException {}
}

class Sensor extends Device {
    Sensor() throws IOException { // VALID: IOException is broader than FileNotFoundException
        super();
    }
}

class UniversalSensor extends Device {
    UniversalSensor() throws Exception { // VALID: Exception is broader than FileNotFoundException
    } // Implicit super() throws FileNotFoundException
}

class BadSensor extends Device {
    // BadSensor() {} // DOES NOT COMPILE: implicit super() throws unreported FileNotFoundException
}

In Sensor(), IOException is broader than FileNotFoundException, which would be too broad for a method overriding a declaration that permits only FileNotFoundException. The explicit Sensor constructor can declare IOException because it covers the exception type declared by Device(). Also note that a subclass constructor is free to declare additional checked exceptions of its own beyond what super(...) throws, because there is no overriding relationship between the constructors. An implicit default constructor is a separate case: it requires an accessible superclass no-argument constructor with no throws clause.

Test Your Knowledge

Assume both classes are in the same package and the exception types are imported. Given class Service { void process() throws IOException {} }, which declaration of process() in WebService extends Service fails?

A

void process() throws FileNotFoundException {}

B

void process() throws Exception {}

C

void process() throws RuntimeException {}

D

void process() {}

Test Your Knowledge

What happens when the following class is compiled and executed?

public class TestNullThrow {
    public static void main(String[] args) {
        try {
            throw null;
        } catch (NullPointerException e) {
            System.out.println("NPE");
        }
    }
}
A

Compilation fails on throw null; because null is not a Throwable instance

B

Compilation succeeds, and running prints nothing because null is ignored

C

Compilation succeeds, and running prints NPE

D

Compilation fails because a NullPointerException cannot be caught after throw null

Test Your Knowledge

Assume both classes are in the same package and the exception types are imported. Given class Base { Base() throws IOException {} } and class Sub extends Base, which constructor in Sub compiles?

A

Sub() throws Exception { super(); }

B

Sub() { try { super(); } catch (IOException e) {} }

C

Sub() throws FileNotFoundException { super(); }

D

Sub() {}

Sections you finish are checked off in the contents.