Errors and Exceptions represent abnormal conditions that disrupt the normal flow of program execution. Although both belong to the Throwable class hierarchy, they differ significantly in cause, handling, and recoverability.

Errors
Errors are serious problems that occur due to system-level failures and are generally beyond the control of the application. They are not meant to be caught or handled by the program.
- Caused by JVM or system failures
- Programs are usually unable to recover

Runtime Errors vs Compile-Time Errors vs and Logical Errors.
public class ErrorExample {
public static void main(String[] args) {
int[] arr = new int[Integer.MAX_VALUE]; // OutOfMemoryError
}
}
Output:
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at ErrorExample.main(Main.java:3)
Explanation:
- The program attempts to allocate an extremely large array, which exceeds the memory limit available to the JVM.
- Due to insufficient heap memory, the JVM throws an OutOfMemoryError during runtime.
- This error is generated by the JVM and is not meant to be handled by application code.
- Once the error occurs, the program terminates abnormally as recovery is not possible.
Exception
Exceptions are abnormal conditions that occur during program execution due to logical or runtime issues. Unlike errors, exceptions can be handled using try-catch blocks.
- Caused by application-level issues
- Can be handled and recovered.

The Exception are of two types:
// User-defined exception
class AgeNotValidException extends Exception{
AgeNotValidException(String message){
super(message);
}
}
public class ExceptionDemo {
// Method that throws a user-defined exception
static void validateAge(int age)
throws AgeNotValidException
{
if (age < 18) {
throw new AgeNotValidException(
"Age must be 18 or above");
}
System.out.println("Age is valid");
}
public static void main(String[] args)
{
// Built-in exception example
try {
int result = 10 / 0; // ArithmeticException
}
catch (ArithmeticException e){
System.out.println("Built-in Exception caught: "
+ e);
}
// User-defined exception example
try {
validateAge(16);
}
catch (AgeNotValidException e) {
System.out.println(
"User-defined Exception caught: "
+ e.getMessage());
}
}
}
Output
Built-in Exception caught: java.lang.ArithmeticException: / by zero User-defined Exception caught: Age must be 18 or above
Explanation:
- The first try-catch block handles a built-in exception (ArithmeticException) caused by division by zero.
- A user-defined exception (AgeNotValidException) is created by extending the Exception class.
- The validateAge() method explicitly throws the custom exception when the condition fails.
- Both exceptions are caught and handled gracefully, demonstrating proper exception handling.
Errors vs Exceptions
| Errors | Exceptions |
|---|---|
| Recovering from Error is not possible. | We can recover from exceptions by either using try-catch block or throwing exceptions back to the caller. |
| All errors in java are unchecked type. | Exceptions include both checked as well as unchecked type. |
| Errors are mostly caused by the environment in which program is running. | Program itself is responsible for causing exceptions. |
Errors can occur at compile time. | Unchecked exceptions occur at runtime whereas checked exceptions occur at compile time |
| They are defined in java.lang.Error package. | They are defined in java.lang.Exception package |
| Examples : java.lang.StackOverflowError, java.lang.OutOfMemoryError | Examples : Checked Exceptions : SQLException, IOException Unchecked Exceptions : ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException. |