Exception Handling
Exception Handling in Java is a powerful mechanism that handles runtime errors to maintain the normal flow of application execution. Java provides a robust and object-oriented way to handle exceptions using try
, catch
, throw
, throws
, and finally
blocks.
Why Use Exception Handling?
- Avoid abnormal termination of the program.
- Gracefully handle unexpected situations like file not found, divide by zero, etc.
- Help in debugging and maintaining the code.
Keywords in Exception Handling
- try – Defines a block of code to test for errors.
- catch – Defines a block of code to handle errors.
- finally – Defines a block of code that will always execute after try/catch, regardless of the outcome.
- throw – Used to explicitly throw an exception.
- throws – Declares exceptions that a method might throw.
Example:
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This will always execute.");
}
}
}
Output:
Error: / by zero
This will always execute.
Types of Exceptions
- Checked Exceptions – Must be handled at compile time (e.g., IOException).
- Unchecked Exceptions – Occur during runtime and are not required to be caught (e.g., ArithmeticException, NullPointerException).
- Errors – Serious problems not intended to be caught (e.g., OutOfMemoryError).