Part 4: Exception Handling, I/O, and Recursion
The Goddess Chalchihuitlicue, found in the Valley of Mexico, 1300-1500 AD (stone), Aztec / Musée de l'Homme, Paris, France / Bridgeman Art Library
This part introduces the use of exception handling and assertions to make your programs robust and correct, the use of input and output to manage and process a large quantity of binary data, and the use of recursion to write methods for solving inherently recursive problems.
[Page 576]
Prerequisites for Part 4
All the chapters after Chapter 17 are designed to minimize dependencies so that they can be reordered flexibly. Chapter 17, "Exceptions and Assertions," can be covered after Chapter 9, "Inheritance and Polymorphism." Chapter 18, "Binary I/O," is usually covered after Chapter 17. The concept of recursion and how to write simple recursive programs in §§19.1–19.3 can be covered after Chapter 6, "Arrays."
Chapter 17 Exceptions and Assertions Chapter 18 Binary I/O Chapter 19 Recursion
[Page 577]
Chapter 17. Exceptions and Assertions
Mayan God Shel, Mexico. Photographer: Philip Coblentz. Courtesy Brand X Pictures
Objectives
To understand exceptions and exception handling (§§17.2–17.3). To distinguish exception types: Error (fatal) vs. Exception (non-fatal), and checked vs. unchecked (§17.3). To declare exceptions in a method header (§17.4.1). To throw exceptions in a method (§17.4.2). To write a try-catch block to handle exceptions (§17.4.3). To explain how an exception is propagated (§17.4.3). To use the finally clause in a try-catch block (§17.5). To know when to use exceptions (§17.6). To rethrow exceptions in a try-catch block (§17.7). To create chained exceptions (§17.8). (Optional) To declare custom exception classes (§17.9). (Optional) To apply assertions to help ensure program correctness (§17.10).
[Page 578]
17.1. Introduction
Three categories of errors (syntax errors, runtime errors, and logic errors) were introduced in