Types of Exceptions in Java
In Java, exceptions are events that disrupt the normal flow of a program. They are classified into different types based on when they occur and how they should be handled.

Checked Exceptions (Compile-Time Exceptions)
- Checked exceptions are verified by the compiler at compile-time.
- Must be handled using try-catch or throws, otherwise the program wonāt compile.
- Usually occur due to external factors like files, network, or database errors.
Common Examples:
| Exception | Cause |
|---|---|
IOException | File read/write errors |
SQLException | Database access issues |
FileNotFoundException | File does not exist |
ClassNotFoundException | Class not found |
InterruptedException | Thread interruption |
Example:
import java.io.*;
public class CheckedExample {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("data.txt"); // Checked Exception
}
}
Unchecked Exceptions (Runtime Exceptions)
- Occur during program execution; not checked at compile-time.
- Usually caused by programmer mistakes, invalid logic, or improper use of objects.
Common Examples:
| Exception | Cause |
|---|---|
ArithmeticException | Divide by zero |
NullPointerException | Using null object |
ArrayIndexOutOfBoundsException | Invalid array index |
NumberFormatException | Invalid string-to-number conversion |
ClassCastException | Wrong type casting |
Example:
public class UncheckedExample {
public static void main(String[] args) {
int result = 10 / 0; // Runtime Exception
}
}
Errors (Not Exceptions)
- Errors are serious problems in JVM that are generally non-recoverable.
- Should not be handled by the program; usually indicate system failure.
Common Examples:
| Error | Cause |
|---|---|
OutOfMemoryError | JVM heap memory full |
StackOverflowError | Infinite recursion |
VirtualMachineError | JVM internal failure |
Example:
public class ErrorExample {
public static void recursive() {
recursive(); // Infinite recursion
}
public static void main(String[] args) {
recursive(); // StackOverflowError
}
}
Exception Hierarchy
Throwable
ā
āāā Exception (Recoverable)
ā āāā Checked Exception
ā āāā Unchecked Exception (RuntimeException)
ā
āāā Error (Non-Recoverable)
Comparison Table
| Type | Checked By Compiler? | Occurrence | Examples | Can Handle? |
|---|---|---|---|---|
| Checked | Yes | Compile-time | IOException, SQLException | Yes |
| Unchecked | No | Runtime | NullPointerException, ArithmeticException | Yes |
| Error | No | JVM failure | OutOfMemoryError, StackOverflowError | No |
Points to Remember:
- Checked exceptions are predictable and must be handled.
- Unchecked exceptions are mostly due to logical errors.
- Errors indicate serious JVM problems that cannot be recovered.
- Exception handling ensures program stability and prevents crashes.
