What is the Difference Between Error and Exception in Java?
Errors are serious problems that are typically not recoverable and occur at the system level. Exceptions are conditions that a program should try to catch and handle.
1. Errors (java.lang.Error)
Indicate serious problems that should not be caught. Usually caused by the environment (JVM, hardware).
public class ErrorExample {
public static void recursiveMethod() {
recursiveMethod(); // No base condition - causes StackOverflowError
}
public static void causeOutOfMemoryError() {
int[] hugeArray = new int[Integer.MAX_VALUE]; // OutOfMemoryError
}
public static void main(String[] args) {
try {
recursiveMethod();
} catch (StackOverflowError e) {
System.out.println("Error caught: " + e);
// Program may not recover properly after this
}
System.out.println("Program may be unstable after error");
}
}
Common Errors
- StackOverflowError - Infinite recursion or too deep call stack
- OutOfMemoryError - JVM runs out of heap memory
- VirtualMachineError - JVM internal error
- AssertionError - Assertion failure
2. Exceptions (java.lang.Exception)
Conditions that a program should catch and handle. Can be checked or unchecked.
public class ExceptionExample {
public static void main(String[] args) {
// Exceptions can and should be handled
try {
String str = null;
System.out.println(str.length()); // NullPointerException
} catch (NullPointerException e) {
System.out.println("Recovered from exception: " + e);
// Program can continue normally
}
System.out.println("Program continues normally after exception");
// ArithmeticException example
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Handled arithmetic exception");
}
System.out.println("Program completes successfully");
}
}
Complete Comparison Example
public class ErrorVsExceptionDemo {
// EXCEPTION - Recoverable
public static void handleException() {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception handled: " + e.getMessage());
// Program recovers and continues
}
System.out.println("Program continues after exception");
}
// ERROR - Usually non-recoverable
public static void causeStackOverflow() {
causeStackOverflow(); // Infinite recursion
}
public static void causeOutOfMemory() {
@SuppressWarnings("unused")
int[] hugeArray = new int[Integer.MAX_VALUE];
}
public static void main(String[] args) {
System.out.println("=== EXCEPTION DEMO ===");
handleException();
System.out.println("
=== ERROR DEMO ===");
try {
// Uncomment to test (will crash if not handled properly)
// causeStackOverflow();
// causeOutOfMemory();
System.out.println("Errors are typically fatal");
} catch (StackOverflowError e) {
System.out.println("Error caught but program may be unstable");
}
}
}
Hierarchy Visualization
// Simplified exception hierarchy
/*
Throwable
├── Error (unchecked, fatal)
│ ├── OutOfMemoryError
│ ├── StackOverflowError
│ └── VirtualMachineError
│
└── Exception
├── RuntimeException (unchecked)
│ ├── NullPointerException
│ ├── ArithmeticException
│ └── IllegalArgumentException
│
└── Checked Exceptions
├── IOException
├── SQLException
└── ClassNotFoundException
*/
Key Differences Table
| Aspect | Error | Exception |
|---|---|---|
| Recoverable? | Usually not | Yes (in most cases) |
| Cause | System/JVM problems | Application/Programming issues |
| Should catch? | No (not recommended) | Yes |
| Hierarchy | java.lang.Error | java.lang.Exception |
| Examples | OutOfMemoryError, StackOverflowError | IOException, NullPointerException |
Understanding Error vs Exception is crucial. Learn more at Online Learner!
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
