In Java, an Error signals a serious problem in the JVM or environment that an application should not try to handle, such as OutOfMemoryError. An Exception signals a condition that your code can reasonably catch and handle, such as a missing file or invalid input. Both extend Throwable, but they are meant for different jobs, and the difference between error and exception in Java affects what you catch.
This guide shows where each sits in the hierarchy, the common examples, and when catching one is justified.
Every object you can throw or catch in Java is a java.lang.Throwable. It has two main subclasses.
Only objects that are instances of Throwable can appear in a throw statement or a catch clause.
An Error indicates a failure that is usually outside the application's control and often not recoverable. The JVM throws most of them. Typical examples:
OutOfMemoryError: the heap (or another memory area) is exhausted.StackOverflowError: the call stack is too deep, usually from unbounded recursion.NoClassDefFoundError: a class that existed at compile time is missing at run time.AssertionError: an assert statement failed, or a library signals a broken invariant.This runs, but it is a demonstration. The real fix for a StackOverflowError is to correct the recursion or make it iterative, not to catch it.
An Exception describes a condition that arises from the program's own logic or from interaction with the outside world, and that code can often react to. There are two kinds:
IOException: the compiler requires you to catch or declare them.RuntimeException, such as IllegalStateException: not enforced, usually bugs.Here the program recovers by using a default, which is the intended use of an exception.
| Aspect | Error | Exception |
|---|---|---|
| Parent | Throwable | Throwable |
| Meaning | Serious JVM or system problem | Recoverable or programmer-handled condition |
| Thrown by | Mostly the JVM | Your code or libraries |
| Checked by compiler | No | Checked: yes. RuntimeException: no |
| Should you catch it? | Rarely | Yes, when you can handle it |
| Examples | OutOfMemoryError, StackOverflowError | IOException, NullPointerException |
Usually no. After an OutOfMemoryError the JVM state may be inconsistent, and catching it hides a real capacity problem. There are a few legitimate cases:
AssertionError.Avoid catch (Throwable t) in normal code, because it also swallows Error types you cannot meaningfully handle. Catch Exception or, better, a specific subclass.
You normally throw exceptions, not errors. Application code should not extend Error.
Throwing an Error yourself is reserved for situations like "this code path must never run", where AssertionError or IllegalStateException is the usual choice.
No, it is an Error, and it extends VirtualMachineError. A catch (Exception e) block will not catch it.
Unchecked. You do not need to declare or catch them, although the language allows it.
Throwable is the root class for everything throwable, while Exception is one branch of it. Catching Throwable also catches every Error.
Sometimes, if the failure was caused by one large allocation that you can release. In general, treat it as fatal, find the memory problem and fix it, for example with a heap dump.
Error and Exception are both subclasses of Throwable. Errors report severe JVM or system failures that you generally do not catch. Exceptions report conditions your code can handle, and are split into checked and unchecked. Catch specific exceptions, avoid Throwable, and fix the cause behind errors such as StackOverflowError.