Ch 11: Risky Behavior
Source: Head First Java, Second Edition | Pages: 349-386
🎯 Learning Objectives
Exception handling
📚 Key Concepts
try-catch blocks
Multiple catch blocks
finally block
Throwing exceptions
Ducking (declaring) exceptions
Checked vs unchecked exceptions
Making your own exceptions
Exception hierarchy
📖 Detailed Notes
1. try-catch blocks
Essential concept for mastering Java and OOP.
Example:
2. Multiple catch blocks
Essential concept for mastering Java and OOP.
Example:
3. finally block
Essential concept for mastering Java and OOP.
Example:
4. Throwing exceptions
Essential concept for mastering Java and OOP.
Example:
5. Ducking (declaring) exceptions
Essential concept for mastering Java and OOP.
Example:
6. Checked vs unchecked exceptions
Essential concept for mastering Java and OOP.
Example:
7. Making your own exceptions
Essential concept for mastering Java and OOP.
Example:
8. Exception hierarchy
Essential concept for mastering Java and OOP.
Example:
💡 Important Points to Remember
cleanup code in one place instead of
if exceptions were of type Broccoli.
from your polymorphism chapters that
from the polymorphism chapters means the object is from a
on a piano! (OK, maybe not someone, but something.)
✅ Self-Check Questions
Test your understanding:
Can you explain the main concepts covered in this chapter?
Can you write code examples demonstrating these concepts?
Do you understand when and why to use these features?
Can you explain the benefits and tradeoffs?
🔄 Quick Revision Points
📝 Practice Exercises
Write your own code examples for each key concept
Modify existing examples to test edge cases
Explain concepts to someone else
Create a small project using these concepts
🔗 Related Chapters
Review related concepts from other chapters to build comprehensive understanding.
For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 349-386.
Chapter 11: Risky Behavior — Study Notes
This chapter introduces Exception Handling in Java. It teaches how to write code that deals with errors gracefully, ensuring that your program doesn't crash when something unexpected happens (like a file being missing or a server being down).
1. The Risky Method
Some methods in Java are considered "risky" because they interact with things outside your program's control (e.g., files, network connections).
The Problem: If you call a risky method, it might fail at runtime.
The Solution: You must acknowledge the risk. Java enforces this by requiring you to wrap the risky call in a
try/catchblock or declare that your method "ducks" the exception.
2. Try/Catch Blocks
The try/catch block is how you handle exceptions. You put the risky code in the try block and the recovery code in the catch block.
Java
Flow Control:
If the code in
trysucceeds, thecatchblock is skipped.If the code in
tryfails (throws an exception), the rest of thetryblock is skipped, and execution jumps immediately to thecatchblock.
3. Checked vs. Unchecked Exceptions
Not all exceptions are treated equally by the compiler.
Checked Exceptions: The compiler forces you to handle these. They typically represent failures you cannot predict or prevent (e.g.,
FileNotFoundException).Unchecked Exceptions (RuntimeExceptions): The compiler does not check these. They usually represent logic errors in your code (e.g.,
NullPointerException,ArrayIndexOutOfBoundsException). You can catch them, but you are not required to.
4. The finally Block
finally BlockA finally block is used for code that must run no matter what—whether the exception happens or not.
Usage: cleaning up resources like closing a file or network connection.
Java
5. Ducking Exceptions
If you don't want to handle an exception in your method, you can "duck" it by declaring that your method throws it. This passes the responsibility to the method that called you.
Java
6. Exception Polymorphism
Hierarchy: All exceptions inherit from
Throwable. You can catch multiple specific exceptions or catch a generalExceptionsuperclass.Order Matters: If you have multiple catch blocks, put the most specific exceptions (subclasses) first and the most generic (superclasses) last. If you catch
Exceptionfirst, it will catch everything, and the specific blocks below it will never run.
7. Revision Checklist
Syntax: Do you have your
try,catch, and optionalfinallyblocks in the correct order?Declaration: If you call a method that throws a checked exception, have you either caught it or declared that you throw it?
Scope: Remember that variables declared inside a
tryblock are local to that block and cannot be accessed in thecatchorfinallyblocks.printStackTrace(): Use this method on the exception object in your catch block to see a trace of where the error occurred.
Last updated