CHAPTER 19
Beginner
Exception Handling
Updated: May 17, 2026
5 min read
# CHAPTER 19
Exception Handling
1. Introduction
Exceptions are unexpected events that disrupt normal program flow — dividing by zero, opening a missing file, or accessing an invalid array index. Without handling them, your program crashes. Exception handling lets you anticipate errors and respond gracefully.2. The Exception Hierarchy
3. try-catch Block
java
4. Multiple catch Blocks
java
5. The finally Block
Always executes, whether an exception occurs or not — used for cleanup (closing files, connections).
java
6. throw and throws
-
throw: Manually throw an exception.
-
throws: Declare that a method might throw an exception.
java
7. Checked vs Unchecked Exceptions
| Type | When | Example |
|---|---|---|
| Checked | Must handle at compile time | IOException, SQLException |
| Unchecked | Runtime errors | NullPointerException, ArithmeticException |
8. Custom Exceptions
java
9. try-with-resources (Java 7+)
Automatically closes resources:
java
10. Mini Project: Safe Division Calculator
java
11. MCQ Quiz with Answers
Question 1
Which block always executes?
Question 2
Checked exceptions must be:
Question 3
NullPointerException is:
Question 4
throw is used to:
Question 5
Custom exceptions extend:
Question 6
try-with-resources automatically:
Question 7
Which is NOT an unchecked exception?
Question 8
throws is used in:
Question 9
What does e.getMessage() return?
Question 10
Can catch blocks be chained?
12. Interview Questions
-
Q: Difference between
throwandthrows?
- Q: Difference between checked and unchecked exceptions?
-
Q: What is the purpose of
finally?
- Q: When would you create a custom exception?
13. Summary
Exception handling prevents crashes by catching errors gracefully. Use try-catch-finally for structured handling.throw creates exceptions; throws declares them. Custom exceptions improve error communication. try-with-resources auto-closes resources.