One handler for two unrelated exceptions
When the division fails, nothing is printed for that pair: the exception escapes while the string is still being built.
A multi-catch, catch (A | B e), handles several unrelated exception types with one block. Rethrowing passes an exception on after doing something with it, and Java 7's precise rethrow lets you catch broadly while still declaring only the exceptions that can really occur.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a method describe(Object o) that does ((String) o).charAt(5) and uses one multi-catch for ClassCastException | StringIndexOutOfBoundsException to return "invalid: " + simple class name. Test with "hello world", 42 and "hi".
Write static void load(String name) throws java.io.IOException, InterruptedException whose body calls helpers declared to throw those two, catches Exception e, prints cleanup for <name>, and rethrows. Show it working for a name that triggers each failure.
Write a withRetry that retries only IllegalStateException (temporary) but rethrows IllegalArgumentException (permanent) immediately. Show both behaviours.
Explain it without notes
What is multi-catch, and what are its restrictions?
What is the static type of a multi-catch parameter, and why is it final?
What is precise rethrow, and when does it stop working?
When should you rethrow, wrap, or handle an exception?
Expected output
10/2 = 5
bad input 10/zero -> NumberFormatException
bad input 10/0 -> ArithmeticException
bad input null/1 -> NumberFormatException