Basics Of Exceptions
1. What Is an Exception?
-
An exception is an event that occurs during program execution and disrupts the normal flow of the program.
-
When an error occurs, Java creates an exception object containing information about the problem.
-
Creating and sending this object is called throwing an exception.
int result = 10 / 0;
This causes Java to throw an exception because division by zero is not valid for integers.
2. How Java Handles an Exception
When an exception is thrown:
Exception occurs
↓
Exception object is created
↓
Java searches for a handler
↓
Current method
↓
Calling method
↓
Further up the call stack
↓
Handler found?
↙ ↘
Yes No
↓ ↓
catch Thread terminates
-
Java searches the call stack for an appropriate exception handler.
-
If a suitable
catchblock is found, the exception is caught. -
If no handler is found, the thread terminates.
-
If the terminated thread is the
mainthread, the program ends.
3. Catch or Specify Requirement
For checked exceptions, Java requires you to either:
Catch the exception
try {
// code that may throw an exception
} catch (Exception e) {
// handle exception
}
Or specify the exception
void readFile() throws IOException {
// code that may throw IOException
}
So:
Catch it or declare it with
throws.
If you do neither for a checked exception, the code will not compile.
4. Three Types of Exceptions
Java's exception hierarchy can be simplified as:
Throwable
├── Error
└── Exception
├── RuntimeException
└── Other checked exceptions
A. Checked Exceptions
-
Represent conditions that an application should generally anticipate and recover from.
-
Must satisfy the Catch or Specify Requirement.
-
Examples:
IOException
FileNotFoundException
SQLException
Example:
void readFile() throws IOException {
// ...
}
B. Runtime Exceptions
-
Represent programming errors or incorrect API usage.
-
They occur during runtime.
-
They do not have to be caught or declared.
-
Extend
RuntimeException.
Examples:
NullPointerException
ArrayIndexOutOfBoundsException
IllegalArgumentException
ArithmeticException
Example:
String name = null;
System.out.println(name.length()); // NullPointerException
C. Errors
-
Represent serious problems that applications generally should not try to recover from.
-
Extend
Error. -
They are not subject to the Catch or Specify Requirement.
Examples:
OutOfMemoryError
StackOverflowError
5. Checked vs. Unchecked Exceptions
| Type | Must catch/declare? | Typical meaning |
|---|---|---|
| Checked Exception | Yes | Expected/recoverable condition |
RuntimeException |
No | Programming/API error |
Error |
No | Serious system/JVM problem |
RuntimeException and Error are collectively called unchecked exceptions.
Throwable
│
├── Error ──────────────── Unchecked
│
└── Exception
│
├── RuntimeException ─ Unchecked
│
└── Other Exceptions ─ Checked
6. Why Does Java Have Checked Exceptions?
Checked exceptions force developers to explicitly think about certain failures.
For example:
void readFile() throws IOException
tells whoever calls the method:
"This method may fail because of an I/O problem. You need to deal with it."
This helps make error handling explicit and improves program reliability.
7. Important Terms
| Term | Meaning |
|---|---|
| Exception | Event that disrupts normal execution |
| Throw | Create/send an exception |
| Catch | Handle an exception |
| Exception handler | Code that handles an exception |
| Call stack | Chain of methods that led to the current execution |
| Checked exception | Must be caught or declared |
| Unchecked exception | Does not have to be caught or declared |
throws |
Declares that a method may throw an exception |
try |
Contains code that may throw an exception |
catch |
Handles a thrown exception |
8. Key Idea to Remember
The most important distinction is:
Checked Exception
↓
Must catch OR declare
RuntimeException / Error
↓
No catch-or-specify requirement
So, in simple terms:
Exceptions are Java's mechanism for reporting and handling problems during program execution. Checked exceptions must be handled or declared, while runtime exceptions and errors are unchecked.