An exception is an event that occurs during program execution and disrupts the normal flow of the program.
All exceptions and errors in Java inherit from the class:
java.lang.Throwable
Throwable has two major branches:
Throwable
├── Error (Serious issues, not handled by programmer)
└── Exception
├── Checked Exceptions
└── Unchecked Exceptions (Runtime Exceptions)
Checked exceptions are checked at compile time. The compiler ensures you handle them using:
try-catch- or
throwskeyword.
If not handled, the program will NOT compile.
IOExceptionSQLExceptionClassNotFoundExceptionFileNotFoundExceptionInterruptedException
public static void main(String[] args) {
FileReader fr = new FileReader("data.txt"); // compile-time error if not handled
}Error: Unhandled exception: FileNotFoundException
try {
FileReader fr = new FileReader("data.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}Or:
void readFile() throws FileNotFoundException {
FileReader fr = new FileReader("data.txt");
}Use them when the caller is expected to recover from the issue (e.g., missing file, database error).
Unchecked exceptions are not checked at compile time. They occur at runtime and usually indicate programming errors.
These exceptions inherit from:
java.lang.RuntimeException
NullPointerExceptionArrayIndexOutOfBoundsExceptionArithmeticExceptionClassCastExceptionNumberFormatException
public static void main(String[] args) {
int x = 10 / 0; // ArithmeticException at runtime
}Compiler DOES NOT force you to handle this.
| Feature | Checked Exceptions | Unchecked Exceptions |
|---|---|---|
| Compile-time checking | ✔ Yes | ❌ No |
| Must be handled? | ✔ Must | ❌ Optional |
| Subclass of | Exception | RuntimeException |
| Common examples | IOException, SQLException | NPE, AIOOBE, ArithmeticException |
| Represents | Expected recoverable issues | Logical programming errors |
| Handling style | Required | Optional |
| Occurs | External environment issues | Programmer mistakes |
Throwable
├── Error
│ ├── OutOfMemoryError
│ └── StackOverflowError
└── Exception
├── Checked Exceptions
│ ├── IOException
│ ├── SQLException
│ └── FileNotFoundException
└── Unchecked Exceptions (RuntimeException)
├── ArithmeticException
├── NullPointerException
├── ArrayIndexOutOfBoundsException
└── ClassCastException
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}String s = null;
System.out.println(s.length()); // NullPointerException-
They force the developer to handle predictable failures
-
Useful for operations involving:
- I/O
- Database
- Network
- Resources
- To indicate bugs in code
- Developer is responsible for fixing the logic
- Handling them usually hides problems instead of solving them
- Caller can fix the problem
- External resource failures
- Example: File not found → user can give another file
- It’s a programming mistake
- Example: Division by zero, accessing null, wrong index
To enforce error handling for recoverable conditions.
Because they usually indicate bugs and should not be forced to handle.
Unchecked.
Yes.
Custom checked:
class MyCheckedException extends Exception {}Custom unchecked:
class MyUncheckedException extends RuntimeException {}-
Checked exceptions → must be handled (compile-time enforcement).
-
Unchecked exceptions → occur at runtime; no compile-time checks.
-
Both extend
Throwable, but unchecked extendRuntimeException. -
Checked = recoverable; Unchecked = programming errors.