Error vs Exception
Aspect
Error
Exception
Definition
Represents serious problems that a reasonable application should not try to catch.
Represents conditions that an application might want to catch and handle.
Type
Subclass of Throwable
.
Subclass of Throwable
.
Recoverability
Non-recoverable; generally cannot be handled by the application.
Recoverable; can be caught and handled using try-catch
.
Examples
OutOfMemoryError
, StackOverflowError
, VirtualMachineError
.
IOException
, NullPointerException
, ArithmeticException
.
Cause
Typically caused by system-level issues or resources.
Typically caused by issues in application logic or external factors.
Handling
Rarely handled as they indicate critical problems in the JVM.
Should be handled in code using try-catch
blocks or declared using throws
.
When to Occur
During the runtime due to system-level failures or resource exhaustion.
During compile-time (checked exceptions) or runtime (unchecked exceptions).
Subtypes
No division into checked or unchecked; all errors are unchecked.
Divided into checked (e.g., IOException
) and unchecked (e.g., NullPointerException
).
Examples in Code
Example: throw new OutOfMemoryError();
Example: throw new IOException("File not found");
or throw new RuntimeException();
.
Purpose
Indicates issues that require JVM intervention or are fatal to the application.
Indicates issues that are part of normal application flow and can be managed.
Last updated
Was this helpful?