11
Exception Handling:
A Deeper Look
Objectives
In this chapter you’ll:
Learn why exception handling
Learn when to use exception
handling.
Understand the exception
class hierarchy.
Use the finally block to
Learn how try-with-resources
can automatically release a
resource when the try block
terminates.
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.
2Chapter 11 Exception Handling: A Deeper Look
Self-Review Exercises
11.1 List five common examples of exceptions.
11.2 Why are exceptions particularly appropriate for dealing with errors produced by methods
of classes in the Java API?
11.3 What is a “resource leak”?
11.4 If no exceptions are thrown in a try block, where does control proceed when the try block
completes execution?
11.5 Give a key advantage of using catch(Exception exceptionName).
11.6 Should a conventional application catch Error objects? Explain.
11.7 What happens if no catch handler matches the type of a thrown object?
11.8 What happens if several catch blocks match the type of the thrown object?
11.9 Why would a programmer specify a superclass type as the type in a catch block?
11.10 What is the key reason for using finally blocks?
11.11 What happens when a catch block throws an Exception?
11.12 What does the statement throw exceptionReference do in a catch block?
11.13 What happens to a local reference in a try block when that block throws an Exception?
Exercises
NOTE: Solutions to the programming exercises are located in the ch11solutions folder.
Each exercise has its own folder named ex11_## where ## is a two-digit number represent-
ing the exercise number. For example, exercise 11.16’s solution is located in the folder
ex11_16.
11.14 (Exceptional Conditions) List the various exceptional conditions that have occurred in pro-
grams throughout this text so far. List as many additional exceptional conditions as you can. For
each of these, describe briefly how a program typically would handle the exception by using the ex
ception-handling techniques discussed in this chapter. Typical exceptions include division by zero
and array index out of bounds.
11.15 (Exceptions and Constructor Failure) Until this chapter, we’ve found dealing with errors
detected by constructors to be a bit awkward. Explain why exception handling is an effective means
for dealing with constructor failure.