Java Exceptions: Try, Catch and Keep Going (Beginner)
The design sheet The full architecture and the verified references. Free, no email.
6:25 docker compose up, and watch it happen
Last episode the product learned to refuse a negative price. But when it refuses, the program stops: make eggs at -5, and nothing after that line runs.
Read the stack trace
Exception in thread "main" java.lang.IllegalArgumentException: price cannot be negative
at Product.setPriceCents(Product.java:20)
at Product.<init>(Product.java:7)
at Checkout.main(Checkout.java:2)
The first line is the kind of problem and the message. The top at line is where it was thrown; the lines below
show how the program got there: main, on line 2, called the constructor, which called setPriceCents.
Catch it and keep going
void main() {
try {
Product eggs = new Product("Eggs", -5);
IO.println("Added " + eggs.getName());
} catch (IllegalArgumentException e) {
IO.println("Could not add eggs: " + e.getMessage());
}
IO.println("Checkout continues");
}
It prints Could not add eggs: price cannot be negative and Checkout continues. Added Eggs never prints: at the
throw, Java leaves the try block, runs the matching catch, and carries on after it, never back at the line that
failed. Catching does not make the bad value good; it lets you decide what happens next. With a price of 30,
nothing is thrown, the catch is skipped, and both lines print.
Java’s own methods throw too: Integer.parseInt("two") throws a NumberFormatException, caught here to print
Not a number: two; with "2" it prints Quantity: 2.
TypeScript
try {
const eggs = new Product("Eggs", -5);
console.log("Added " + eggs.name);
} catch (e) {
if (e instanceof Error) {
console.log("Could not add eggs: " + e.message);
}
}
console.log("Checkout continues");
JavaScript lets you throw anything, so the caught value is unknown: reading e.message straight away is error
TS18046. Checking e instanceof Error first fixes it. And JavaScript’s parseInt("two") does not throw at all: it
returns NaN.
The challenge
Inside a try: print One, Integer.parseInt("two"), print Two. The catch prints Caught; after it, print Three. What
prints? The answer is in the episode and the repository’s README. Next episode: ArrayList, a list that can grow.