An Exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Exception Handling is a powerful mechanism that allows us to catch these runtime errors so the application doesn't crash. In Java, exceptions are objects that encapsulate information about what went wrong and where it happened.
Java uses five primary keywords to manage exceptions. Think of them as a safety net for your code:
catch or finally.Not all exceptions are handled the same way. Java divides them into two main categories based on when the compiler checks for them.
These are checked at Compile-time. The compiler forces you to handle them. They represent conditions outside the control of the program.
Examples: IOException, SQLException, FileNotFoundException.
These occur at Runtime. They are usually the result of bad programming logic and are not checked by the compiler.
Examples: NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException.
When an exception is thrown, the JVM looks for a catch block in the current method. If it doesn't find one, it "pops" the current method off the stack and looks in the caller method. This continues until a handler is found or the program reaches the main method and terminates.
This example demonstrates a multi-catch block and the finally block to ensure resources are closed even if an error occurs.
In the old days, we manually closed files in the finally block. Modern Java introduced Try-With-Resources, which automatically closes any object that implements the AutoCloseable interface.
try (Scanner scanner = new Scanner(new File("test.txt"))) {
// Read file
} catch (FileNotFoundException e) {
// Handle error
} // Scanner is closed automatically here!
For domain-specific errors (like InsufficientFundsException), you can create your own exception by extending the Exception class (for checked) or RuntimeException (for unchecked).
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
FileNotFoundException specifically instead of just catching the top-level Exception.finally or Try-With-Resources to prevent memory leaks.if-else statements.Q: What is the difference between final, finally, and finalize?
A: final is a keyword for constants/non-inheritance; finally is an exception block that always runs; finalize() is a method called by the Garbage Collector before an object is destroyed (deprecated in modern Java).
Q: Can a catch block exist without a try block?
A: No. A catch or finally must immediately follow a try block.
Q: What happens if an exception is thrown in the finally block?
A: It will suppress any previous exception thrown in the try block unless handled, which is why you should be very careful with logic inside finally.
Exception handling is not just about stopping crashes; it's about providing a meaningful way for your program to recover. By mastering the hierarchy and the sophisticated tools like Try-With-Resources, you ensure that your Java applications are professional, reliable, and easy to debug.
Next: Introduction to Multi-Threading →