| Aspect | Error | Exception |
|---|---|---|
| Definition | A serious runtime issue that the application typically cannot recover from (e.g., memory or system failures). | An abnormal, often recoverable event that disrupts program flow, caused by logic or environmental issues. |
| Cause / Origin | Caused by system-level or JVM issues (e.g., memory exhaustion, VM crash). | Caused by Application-level problems such as invalid input, faulty logic, or resource access errors. |
| Recoverability | We cannot recover the error, the program should log it and terminate. |
We can recover the exception using try-catch blocks or throwing exceptions back to the caller.
|
| Types / Categories | System-related only; not divided further in the Exception hierarchy. |
Two categories: • Checked Exceptions (must be handled or declared) • Unchecked Exceptions (runtime exceptions) |
| Examples |
OutOfMemoryError, StackOverflowError, VirtualMachineError
|
Checked: IOException, SQLException Unchecked: NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException
|
| When They Occur | Can occur both at compile time and runtime. | Primarily occur at runtime, though checked exceptions can be detected at compile time. |
| Predictability | Unpredictable and often outside the control of the application. | Can be expected and handled through proper coding practices. |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.