Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Exception class

Reference for java.lang.Exception: how checked exceptions work, the catch-or-declare rule, writing your own checked exception and common JDK subclasses.

java.lang.Exception is the base class for conditions that a reasonable program might catch and handle. Any subclass of Exception that is not also a subclass of RuntimeException is a checked exception: the compiler forces every method that might throw one to either catch it or declare it with throws.

Checked exceptions model failures the caller can plausibly recover from or must at least acknowledge, such as a missing file (FileNotFoundException), a failed network call, or a malformed document. Every constructor and method comes from Throwable; Exception adds no members of its own, only its place in the hierarchy.

Java
class InsufficientFundsException extends Exception {
    InsufficientFundsException(String message) {
        super(message);
    }
}

Try it

Java Declaring and catching a checked exception

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 Hierarchy, multi-catch and wrapping

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
public class Exception extends Throwable
Constructor Description
Exception() No message, no cause.
Exception(String message) Detail message.
Exception(String message, Throwable cause) Message and the exception that caused this one.
Exception(Throwable cause) Wraps a cause; the message becomes cause.toString().

Commonly used checked subclasses: IOException, FileNotFoundException, SQLException, InterruptedException, ClassNotFoundException, CloneNotSupportedException, TimeoutException, ExecutionException, ParseException.

Usage notes

  • Catch or declare. A checked exception thrown in a method body must be caught there or listed in the method's throws clause. A catch for a checked exception that the try body cannot throw is a compile error.
  • Overriding. An overriding method may not declare broader checked exceptions than the method it overrides. Narrower or none is fine.
  • Lambdas. Functional interfaces such as Function do not declare checked exceptions, so wrap them (for example in UncheckedIOException) or catch them inside the lambda.
  • Do not swallow. An empty catch block hides failures; at least log or rethrow with the cause attached.
  • Make them informative. Put the data needed to understand the failure in the message or in fields, as missing() does in the first example.
  • Catching Exception also catches every RuntimeException, which usually hides programming errors. Catch the narrowest type you can handle.
  • throws Exception on main is fine for demos but gives callers no information in library code.
  • Choose checked sparingly. Use them when callers can do something useful; use unchecked ones for bugs. See Checked vs. unchecked.