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.
class InvalidOrderException extends RuntimeException { InvalidOrderException(String message) { super(message); } }
Try it
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.
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
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 nothrowsand notry. This is the only difference from otherExceptionsubclasses. - Treat them as bugs. Validate inputs up front and throw
IllegalArgumentExceptionorNullPointerException(viaObjects.requireNonNull) with a clear message, rather than letting a deep failure surface later. - Do not catch them to hide problems. Catching
RuntimeExceptionis 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
RuntimeExceptionforces 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
throwsfor readers, and Javadoc@throwsis 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.