Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The RuntimeException class

Reference for java.lang.RuntimeException: unchecked exceptions, which JDK subclasses you will meet, how to write your own and when to catch them.

java.lang.RuntimeException is the superclass of unchecked exceptions: exceptions the compiler does not force you to catch or declare. They usually signal programming errors, such as passing a bad argument, dereferencing null, indexing past the end of an array, or calling a method at the wrong time. The fix is to correct the code, not to wrap every call in try.

Because they need no throws clause, runtime exceptions can pass through lambdas, interface methods and long call chains without changing any signatures. That convenience is also the danger: nothing in a method's signature tells you what it might throw, so document it in the Javadoc.

Java
class InvalidOrderException extends RuntimeException {
    InvalidOrderException(String message) {
        super(message);
    }
}

Try it

Java Common unchecked 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 Your own unchecked 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.

Syntax

Java
public class RuntimeException extends Exception
Constructor Description
RuntimeException() No message.
RuntimeException(String message) Detail message.
RuntimeException(String message, Throwable cause) Message and cause.
RuntimeException(Throwable cause) Wraps a cause; message is cause.toString().

Frequently met subclasses: NullPointerException, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, ArrayIndexOutOfBoundsException, ClassCastException, NumberFormatException, ArithmeticException, UnsupportedOperationException, ConcurrentModificationException, NoSuchElementException, DateTimeException, UncheckedIOException.

Usage notes

  • No compile-time enforcement. throw new RuntimeException(...) needs no throws and no try. This is the only difference from other Exception subclasses.
  • Treat them as bugs. Validate inputs up front and throw IllegalArgumentException or NullPointerException (via Objects.requireNonNull) with a clear message, rather than letting a deep failure surface later.
  • Do not catch them to hide problems. Catching RuntimeException is reasonable at a boundary such as a request handler, a thread's top level or a task runner, where you log and continue.
  • Use meaningful types. Throwing plain RuntimeException forces callers to parse the message. Pick the standard subclass that fits, or create a small custom one.
  • Always pass the cause when converting one exception to another, so the original stack trace is not lost.
  • Documenting them is optional but kind. You may list them in throws for readers, and Javadoc @throws is the usual place.
  • Threads. An uncaught runtime exception ends the thread (the default handler prints the trace); in an executor task it is captured in the Future.