Differences between Checked and Unchecked Exceptions at Runtime?
In Java, exceptions are categorized into Checked and Unchecked exceptions. While they differ primarily at compile-time, there are also runtime considerations. Here’s an explanation with examples:
1. Checked Exceptions
Definition:
- Checked exceptions are exceptions that must be declared in a method’s
throwsclause or handled with atry-catchblock at compile-time. - They are subclasses of
Exception, excludingRuntimeException.
Runtime Behavior:
- At runtime, if a checked exception is not handled (though this is usually caught at compile-time), it will propagate up the call stack until it is caught or causes the program to terminate.
- Checked exceptions are often used for recoverable situations (e.g., file not found, network issues).
Example:
import java.io.FileReader;
import java.io.IOException;
public class CheckedExample {
public static void main(String[] args) {
try {
FileReader file = new FileReader("nonexistentfile.txt"); // Throws FileNotFoundException
file.read();
} catch (IOException e) {
System.out.println("Caught Checked Exception: " + e.getMessage());
}
}
}
Output:
Caught Checked Exception: nonexistentfile.txt (No such file or directory)
2. Unchecked Exceptions
Definition:
- Unchecked exceptions are exceptions that are not checked by the compiler at compile-time.
- They are subclasses of
RuntimeExceptionand include exceptions likeNullPointerException,ArrayIndexOutOfBoundsException, andIllegalArgumentException.
Runtime Behavior:
- These exceptions occur due to programming errors or invalid data at runtime.
- They can propagate up the call stack if not caught but are not required to be explicitly handled.
Example:
public class UncheckedExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
try {
System.out.println(numbers[5]); // Throws ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught Unchecked Exception: " + e.getMessage());
}
}
}
Output:
Caught Unchecked Exception: Index 5 out of bounds for length 3
Key Differences at Runtime
| Aspect | Checked Exceptions | Unchecked Exceptions |
|---|---|---|
| Compiler Enforcement | Must be handled or declared in throws. | No compile-time enforcement to handle or declare them. |
| Runtime Propagation | If unhandled, the program will terminate. | If unhandled, the program will terminate. |
| Use Case | Represent recoverable conditions. | Represent programming bugs or developer errors. |
| Common Examples | IOException, SQLException, FileNotFoundException. | NullPointerException, ArrayIndexOutOfBoundsException. |
Best Practices
- Handle Checked Exceptions Properly:
- Use
try-catchblocks or propagate exceptions withthrows.
- Use
public void readFile() throws IOException {
FileReader file = new FileReader("example.txt");
file.read();
}
2. Avoid Silencing Unchecked Exceptions:
- Fix the root cause rather than suppressing unchecked exceptions.
// Instead of catching NullPointerException
String name = null;
if (name != null) {
System.out.println(name.length());
}
3. Use Custom Exceptions:
- Create custom exceptions for specific use cases to improve readability and debugging.
public class InvalidInputException extends RuntimeException {
public InvalidInputException(String message) {
super(message);
}
}
No images available.