Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Error class

Reference for java.lang.Error: serious JVM problems such as OutOfMemoryError, StackOverflowError and AssertionError, and why programs rarely catch them.

java.lang.Error is the branch of Throwable for serious problems that an application is not expected to handle: the JVM running out of memory, the stack overflowing, a class that cannot be linked, or an internal failure. Errors are unchecked, so methods never need to declare them.

Most Error subclasses mean the virtual machine or the program's environment is in trouble, not that a method received bad input. The usual response is to let the error propagate, log it at the top level and shut down or restart. A few, such as AssertionError, are thrown by your own code on purpose.

Java
try {
    recurseForever();
} catch (StackOverflowError e) {
    // possible, but rarely the right fix
}

Try it

Java Catching and surviving a StackOverflowError

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 AssertionError and other Error types

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 Error extends Throwable
Constructor Description
Error() No message.
Error(String message) Detail message.
Error(String message, Throwable cause) Message and cause.
Error(Throwable cause) Wraps a cause.

Notable subclasses:

Class Meaning
StackOverflowError The call stack is exhausted, usually by runaway recursion.
OutOfMemoryError The heap (or another memory area) cannot satisfy an allocation.
AssertionError An assert failed, or code reports an impossible state.
ExceptionInInitializerError A static initializer threw.
NoClassDefFoundError, LinkageError A class that existed at compile time cannot be loaded or linked.
VirtualMachineError, InternalError Failures inside the JVM.

Usage notes

  • Errors are not Exceptions. catch (Exception e) does not catch them; catch (Throwable t) does, which is why that pattern is dangerous.
  • Do not catch to continue normally. After an OutOfMemoryError the program state may be inconsistent, since the failing allocation could have happened anywhere. Catch it only to clean up, log or exit.
  • StackOverflowError is recoverable in practice (the stack unwinds), but fix the recursion or convert it to a loop instead of catching it.
  • AssertionError is legitimate in your own code for "can't happen" branches. Its constructors accept any object, and a Throwable argument becomes the cause.
  • Do not extend Error for application failures. Create exceptions instead.
  • Static-initializer failures are sticky. After ExceptionInInitializerError, later uses of the class throw NoClassDefFoundError.
  • Unchecked, so they need no throws, and they pass through finally blocks like any other throwable.