Sessions 15 & 16: Exception Handling
π Exception Hierarchy
flowchart TB
Throwable["Throwable"]
Throwable --> Error["Error<br/>(Unrecoverable)"]
Throwable --> Exception["Exception<br/>(Recoverable)"]
Error --> oom["OutOfMemoryError"]
Error --> soe["StackOverflowError"]
Error --> vme["VirtualMachineError"]
Exception --> RE["RuntimeException<br/>(Unchecked)"]
Exception --> CE["Checked Exceptions"]
RE --> npe["NullPointerException"]
RE --> aiob["ArrayIndexOutOfBoundsException"]
RE --> iae["IllegalArgumentException"]
RE --> nfe["NumberFormatException"]
CE --> ioe["IOException"]
CE --> sql["SQLException"]
CE --> fnf["FileNotFoundException"]
CE --> cnf["ClassNotFoundException"]
π΄ Errors vs Checked vs Unchecked Exceptions
| Type | Description | Handling | Examples |
|---|---|---|---|
| Error | Serious problems, JVM level | Cannot usually handle | StackOverflowError, OutOfMemoryError |
| Checked Exception | Compile-time exceptions | Must handle or declare | IOException, SQLException |
| Unchecked Exception | Runtime exceptions | Optional handling | NullPointerException, ArithmeticException |
Checked Exception Example
import java.io.*;
public class CheckedDemo {
// Must handle or declare
public void readFile() throws FileNotFoundException {
FileReader fr = new FileReader("test.txt"); // Checked exception
}
// Handling with try-catch
public void readFileSafe() {
try {
FileReader fr = new FileReader("test.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
Unchecked Exception Example
public class UncheckedDemo {
// No need to declare - but can still handle
public void divide(int a, int b) {
int result = a / b; // ArithmeticException if b = 0
}
public void access(int[] arr, int index) {
int value = arr[index]; // ArrayIndexOutOfBoundsException
}
}
π Exception Propagation
When an exception occurs in a method and is not handled, it propagates up the call stack.
public class PropagationDemo {
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
System.out.println("Caught in main: " + e.getMessage());
}
}
static void method1() {
method2();
}
static void method2() {
method3();
}
static void method3() {
int result = 10 / 0; // ArithmeticException
// Exception propagates: method3 β method2 β method1 β main
}
}
flowchart TB
main["main()"] -->|"calls"| m1["method1()"]
m1 -->|"calls"| m2["method2()"]
m2 -->|"calls"| m3["method3()"]
m3 -->|"throws ArithmeticException"| prop["Exception Propagation"]
prop -->|"up the stack"| m2
prop -->|"up the stack"| m1
prop -->|"up the stack"| main
π‘οΈ try-catch-finally Block
Basic Syntax
try {
// Code that might throw exception
int result = 10 / 0;
} catch (ArithmeticException e) {
// Handle specific exception
System.out.println("Cannot divide by zero");
} catch (Exception e) {
// Handle other exceptions
System.out.println("Error: " + e.getMessage());
} finally {
// Always executes (cleanup code)
System.out.println("Finally block executed");
}
finally Block Rules
| Scenario | finally Executes? |
|---|---|
| Normal execution | β Yes |
| Exception caught | β Yes |
| Exception not caught | β Yes |
| return in try/catch | β Yes (before return) |
| System.exit() called | β No |
| JVM crash | β No |
public class FinallyDemo {
public static int getValue() {
try {
return 1;
} finally {
System.out.println("Finally runs!");
// return 2; // Would override return 1 (bad practice)
}
}
public static void main(String[] args) {
System.out.println(getValue()); // Prints: Finally runs! then 1
}
}
π€ throws Clause and throw Keyword
throws (Declaration)
Declares that a method might throw an exception.
public class ThrowsDemo {
// Declaring checked exceptions
public void method1() throws IOException {
throw new IOException("IO Error");
}
// Multiple exceptions
public void method2() throws IOException, SQLException {
// ...
}
// Calling method must handle or propagate
public void caller() {
try {
method1();
} catch (IOException e) {
e.printStackTrace();
}
}
}
throw (Creating Exception)
Explicitly throws an exception.
public class ThrowDemo {
public void validateAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (age > 150) {
throw new IllegalArgumentException("Age too high");
}
System.out.println("Valid age: " + age);
}
public void readFile(String filename) throws FileNotFoundException {
if (filename == null) {
throw new FileNotFoundException("Filename is null");
}
// Read file logic
}
}
throw vs throws
| throw | throws |
|---|---|
| Used inside method | Used in method signature |
| Throws an exception | Declares potential exceptions |
| Followed by exception object | Followed by exception class(es) |
| Can throw only one at a time | Can declare multiple |
π Multi-catch Block (Java 7+)
Handle multiple exception types with a single catch block.
public class MultiCatchDemo {
public void process(String[] args) {
try {
int num = Integer.parseInt(args[0]);
int result = 10 / num;
String str = null;
str.length();
} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
System.out.println("Index or arithmetic error: " + e.getMessage());
} catch (NullPointerException | NumberFormatException e) {
System.out.println("Null or format error: " + e.getMessage());
} catch (Exception e) {
System.out.println("General error: " + e.getMessage());
}
}
}
Multi-catch Rules
- Exception types must not be related (no parent-child)
- Exception variable is implicitly final
- More specific exceptions must come before general
π Try-with-Resources (Java 7+)
Automatically closes resources (implements AutoCloseable).
// Traditional way
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Try-with-resources (cleaner)
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line = br.readLine();
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
// br is automatically closed
π€ User-defined Exceptions
Checked Exception
// Custom checked exception
public class InsufficientBalanceException extends Exception {
private double amount;
public InsufficientBalanceException(String message) {
super(message);
}
public InsufficientBalanceException(String message, double amount) {
super(message);
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
// Usage
public class BankAccount {
private double balance;
public void withdraw(double amount) throws InsufficientBalanceException {
if (amount > balance) {
throw new InsufficientBalanceException(
"Insufficient balance. Required: " + amount, amount);
}
balance -= amount;
}
}
Unchecked Exception
// Custom unchecked exception
public class InvalidAgeException extends RuntimeException {
public InvalidAgeException(String message) {
super(message);
}
}
// Usage - no throws declaration needed
public class Person {
private int age;
public void setAge(int age) {
if (age < 0 || age > 150) {
throw new InvalidAgeException("Invalid age: " + age);
}
this.age = age;
}
}
π‘ Key MCQ Points
- Throwable is parent of both Error and Exception
- Checked exceptions must be handled or declared
- Unchecked exceptions (RuntimeException) - optional handling
- Error - serious, usually unrecoverable (donβt catch)
- finally always executes except for System.exit() or JVM crash
- throw creates exception, throws declares it
- Multi-catch uses
|to combine exception types - Try-with-resources auto-closes AutoCloseable resources
- Specific exceptions catch blocks must come before general
- Custom checked extends Exception, unchecked extends RuntimeException
Exception Handling Order
try {
// Code
} catch (FileNotFoundException e) { // Most specific first
// Handle
} catch (IOException e) { // Parent class second
// Handle
} catch (Exception e) { // Most general last
// Handle
}
Common Exception Methods
| Method | Description |
|---|---|
getMessage() |
Returns error message |
printStackTrace() |
Prints stack trace |
getCause() |
Returns cause of exception |
toString() |
Returns string representation |