Week 13 (Reading | Slides | Exercises)
Reading
- Section 1: What Is an Exception?
- Section 2: Catching and Handling Exceptions?
- Section 3: Throwing Exceptions
- Section 4: Unchecked Exceptions — The Controversy
Slides
Exercises
Exercise 13.01 (Mandatory): Explain – in your own words – what is an exception?
Exercise 13.02: Explain – in your own words – what does a throw statement
do? What does a try-catch statement do? Can you have one without the other?
Exercise 13.03: Explain – in your own words – what is the difference between
a try-catch and a try-catch-finally statement?
Exercise 13.04: Explain – in your own words – what is the difference between an checked and an unchecked exception? When would you use one or the other?
Exercise 13.05 (Mandatory): For each of the following exceptions, mark whether it is a checked or unchecked:
NullPointerExceptionIOExceptionIllegalArgumentExceptionArrayIndexOutOfBoundsExceptionNumberFormatExceptionConcurrentModificationExceptionInterruptedException
How many of these have you experienced?
Exercise 13.06: Write a class to represent a bank account. An account has a
balance. Add deposit and withdraw methods. The balance of the account must
always be non-negative. Write a class InsufficientFundsException, which
extends RuntimeException, and throw this exception if a withdrawal would make
the balance negative.
Exercise 13.07 (Mandatory): Write a class to represent a gearbox with five
gears and a gear for reverse. Add a method changeGear(int gear) to change the
current gear. The method must throw IllegalArgumentException if the gear is
not one of -1, 1, 2, 3, 4, and 5. Here reverse is represented as
-1. Write a class IllegalGearChangeException, which extends
RuntimeException, and throw this exception: (a) when switching from any gear
other than the first gear into reverse (and vice versa), and (b) when skipping
one or more gears. For example, it is illegal to switch directly from the first
gear to the third gear. It is also not allowed to switch directly from reverse
to the fourth gear.
Exercise 13.08: Write a class to represent a digital display (use Google
images for examples) with four digits. Add a method getDigit(int i) to return
the value of the ith digit. Add a method setDigit(int i, int v) to change
the value of the ith digit to v. Add two exception classes:
NoSuchDigitException and IllegalDigitException and throw these when
appropriate.
Exercise 13.09 (Mandatory): Write a class to represent a printer from
hell. The class should have a single method print(). Whenever this method is
called, the printer randomly throws one of the following exceptions:
OutOfPaperException, OutOfTonerException, PaperJamException. Write classes
for these exceptions. Write a main method, which calls print(), catches any
exception, prompts the user to take action (e.g. "replace toner"), waits for
confirmation from the user, and then calls print() again. Bonus points for
infuriating or vaguely worded instructions.
Exercise 13.10: Write a class to represent a car. Add methods to (a) press the clutch, (b) release the clutch, (c) turn on the ignition, (d) turn off the ignition, (e) pull the handbrake, and (f) release the handbrake. To correctly turn on the car, the following steps should be taken in order: (1) press the clutch, (2) turn on the ignition, (3) release the handbrake, and (4) release the clutch. Add appropriate exceptions and throw these if the car is operated incorrectly.
Exercise 13.11: Write a class to represent a dishwasher. A dishwasher contains a number of glasses and plates. Add methods to open and close the dishwasher door, to turn on the dishwasher, and to select the program (eco-friendly, intense, etc.). Add methods to insert and remove glasses/plates from the dishwasher. Add appropriate exceptions and throw these to ensure that: (a) glasses and plates can only be removed when the dishwasher is open (b) the dishwasher cannot be opened when it has been turned on, and (c) a program must be selected before the dishwasher can be turned on.
Exercise 13.12: Write classes to represent
a faucet, a bucket, and a water stream.
A bucket is either full or empty.
A water stream has a source (its faucet).
Add methods to turn the faucet on and off.
When a faucet is turned on, it yields a new water stream.
A water stream can be used to fill multiple buckets when active.
Add a method to the bucket to fill it.
If the bucket is already full, throw an appropriate exception.
Use AutoClosable to ensure that the faucet is always turned off,
even when there is an exception.
Write a main method using try-with-resources
that demonstrates the faucet turning off automatically.
(Hint: compare this with files and FileInputStream.)
Exercise 13.13: Write a program to represent a library network. A library network has two libraries, a library has two shelves, and a shelf has two books. A book has an ISBN, a name, and a checked-out status. Add a method to the library network, the library, and the shelf to check out a book and return its ISBN. If the book is not present, throw an exception to indicate the missing book. If the book is checked out already, throw an exception to indicate this. Remember that a book might be available at a different library. Write a main method constructing a library network and demonstrating what happens when: (a) we successfully check out a book (b) we successfully check out two different copies of the same book (c) we fail to check out a book because all copies are checked out (d) we fail to check out a book because it is not present in any library.