githubEdit

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:

  1. Can you explain the main concepts covered in this chapter?

  2. Can you write code examples demonstrating these concepts?

  3. Do you understand when and why to use these features?

  4. Can you explain the benefits and tradeoffs?

🔄 Quick Revision Points


📝 Practice Exercises

  1. Write your own code examples for each key concept

  2. Modify existing examples to test edge cases

  3. Explain concepts to someone else

  4. Create a small project using these concepts

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/catch block 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 try succeeds, the catch block is skipped.

    • If the code in try fails (throws an exception), the rest of the try block is skipped, and execution jumps immediately to the catch block.

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

A 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 general Exception superclass.

  • Order Matters: If you have multiple catch blocks, put the most specific exceptions (subclasses) first and the most generic (superclasses) last. If you catch Exception first, it will catch everything, and the specific blocks below it will never run.

7. Revision Checklist

  • Syntax: Do you have your try, catch, and optional finally blocks 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 try block are local to that block and cannot be accessed in the catch or finally blocks.

  • printStackTrace(): Use this method on the exception object in your catch block to see a trace of where the error occurred.

Last updated