Jan 24, 2026
## Chapter 13: Exceptions (The Safety Net)
An exception is an "unexpected guest." You have to deal with them, or they’ll ruin the party.
### 1. The `try-catch` Net
**Example 1: Division by Zero (Basic Example)**
```java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Oops! Can't do that.");
}
```
**Example 2: The Generic Catch (Common Mistake)**
```java
try { ... } catch (Exception e) { } // BAD!
```
*Teacher's Note:* Catching everything with `Exception` is like a doctor saying "You're sick" without saying why. Be specific!
### 2. `finally`: The Cleanup
**Example 3: The Library Book (Real-Life Analogy)**
Whether you finished the book or it was too hard to read, you **must** return it to the library.
```java
try { ... } finally {
System.out.println("Closing file...");
}
```
### 3. Checked vs Unchecked
**Example 4: Surprise vs Planned (Explanation Focused)**
- **Unchecked**: Surprise bugs (like `NullPointerException`).
- **Checked**: External problems (like a missing file). Java forces you to plan for these.
**Example 5: Custom Exceptions (Future Hint)**
```java
class AgeTooLowException extends Exception { ... }
```
*Teacher's Note:* Later, you'll create your own errors to make your program even smarter!