Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The throw statement and throws clause

Reference for Java throw and throws: raising exceptions, checked versus unchecked exceptions, declaring them in method signatures and overriding rules.

throw raises an exception: it takes a reference to a Throwable object and starts unwinding the call stack until a matching catch is found. throws is different: it is part of a method or constructor signature and lists the checked exceptions that the caller must handle or pass on.

Java divides exceptions into checked ones (subclasses of Exception that are not RuntimeException), which the compiler tracks, and unchecked ones (RuntimeException, Error and their subclasses), which it does not.

Java
void withdraw(int amount) throws InsufficientFundsException {
    if (amount > balance) {
        throw new InsufficientFundsException(amount - balance);
    }
    balance -= amount;
}

Try it

Java Throwing unchecked and checked exceptions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Rethrowing, wrapping and throw expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
throw expression;                        // expression is a Throwable
returnType name(params) throws E1, E2 { ... }
Name(params) throws E1 { ... }           // constructors too
Part Description
throw operand A non-null reference to Throwable or a subclass. throw null; compiles but throws NullPointerException.
throws list Checked exception types the method can let escape. Unchecked ones may be listed but are not required.

Usage notes

  • Checked rule: if the body can throw a checked exception that is not caught, it must be declared in throws, otherwise the code does not compile.
  • Overriding: an overriding method cannot declare broader checked exceptions than the method it overrides; it can declare fewer or narrower ones, or none.
  • Lambdas: functional interfaces such as Function declare no checked exceptions, so a lambda cannot throw them. Wrap in an unchecked exception or define your own interface with a throws clause.
  • Choose the type with care: use IllegalArgumentException for bad arguments, IllegalStateException for wrong object state, UnsupportedOperationException for unsupported operations, and custom checked exceptions only when callers can reasonably recover.
  • Give useful messages that include the offending value. Pass the original exception as the cause when wrapping.
  • The stack trace is captured where the exception is created, not where it is thrown. Creating one and throwing it later shows the creation point.
  • Declaring throws Exception on everything defeats checking and forces callers to catch broadly.
  • throw ends the statement: code after an unconditional throw in the same block is unreachable.
  • In static initializers only unchecked exceptions are allowed; they surface as ExceptionInInitializerError.