Throwing Exceptions and Cleaning Up
A method throws an exception when it detects a problem it cannot fix, custom exceptions give such problems a precise name, and try-with-resources closes resources automatically.
Add useful failure context and use resource cleanup that still runs on failure.
So far we have caught exceptions that the library threw. Brew Bar's own rules need the same power: an order for 50 cups, or a drink that has run out, should stop the operation with a clear message. And the receipt printer must always be released, however the order ends.
The rule
Throw where it breaks, catch where you can fix it.
The method that discovers a problem is usually the wrong place to decide what to do about it. So it throws, and a caller higher up, such as the screen that can show a message, catches:
public void addCups(int extra) {
if (extra < 1 || cups + extra > MAX_CUPS) {
throw new IllegalArgumentException("cups must stay between 1 and " + MAX_CUPS);
}
cups += extra;
}Keep the two keywords apart: throw (in the body) actually throws one exception object; throws (in the header) declares that a method might let one escape.
When no standard exception describes your situation, create your own by extending an existing class:
public class OutOfStockException extends Exception {
public OutOfStockException(String drink) {
super(drink + " is out of stock");
}
}Extend Exception for a checked exception, when callers should be forced to plan for it, as with running out of oat milk. Extend RuntimeException for an unchecked one, typically for misuse of your class. A named exception type lets callers catch exactly this problem and nothing else.
For clean-up, try-with-resources is better than a hand-written finally. Anything that implements AutoCloseable, such as files, network connections or Brew Bar's ReceiptPrinter, can be opened in brackets after try, and Java closes it automatically:
try (ReceiptPrinter printer = new ReceiptPrinter();
CashDrawer drawer = new CashDrawer()) {
printer.print(order.receipt());
drawer.open();
} // drawer closes, then printer, even if an exception was thrownResources are closed in reverse order of opening, like stacking and unstacking plates.