Member-only story
Can You Catch an Error
in Java | Tricky Java Interview Questions — Part 17
Hello Developers,
Welcome to Part 17 of the Tricky Java Interview Questions series.
Today’s question explores Java’s exception mechanism — specifically around the often-confused class called Error
:
Can you catch an
Error
in Java?
Let’s cut straight to it:
✅ Short Answer
Yes, you can catch an
Error
in Java using acatch
block.
But it’s almost always a bad idea to do so.
Let’s understand what Error
really is, and why Java allows it — even though you're almost never supposed to use it that way.
Java Exception Hierarchy Recap
In Java, everything that can be thrown inherits from Throwable
.
Throwable
/ \
Error Exception
- Exception: For conditions your code should try to handle (e.g.,
IOException
,NullPointerException
) - Error: For conditions your code should NOT try to handle (e.g.,
OutOfMemoryError
,StackOverflowError
)
✅ Yes, Error
Is Catchable
public class CatchErrorExample {
public static void main(String[] args) {
try {
throw new StackOverflowError("Custom SOE");
} catch (Error e) {
System.out.println("Caught Error: "…