Multiple catch blocks
18 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)One try block can have several catch blocks, one for each kind of problem. Java runs the first catch block that matches the exception, and only that one.
One catch for each problem
Splitting a bill can fail in two ways: the number of friends is not a number, or it is 0. Write one catch block per type, one under the other; Java checks them from the top and runs the first one that matches.
Example 1 · Two problems, two catch blocks
public class SplitInputs { public static void main(String[] args) { String[] inputs = {"3", "0", "abc"}; for (String text : inputs) { try { int friends = Integer.parseInt(text); System.out.println("Each pays: " + 900 / friends); } catch (NumberFormatException e) { System.out.println(text + " is not a number"); } catch (ArithmeticException e) { System.out.println("Cannot split among 0 friends"); } } }}Each pays: 300 Cannot split among 0 friends abc is not a number
What this code does
"3"works, so no catch block runs."0"converts fine, but900 / friendsthrows. The first block does not match, so Java tries the next:ArithmeticExceptionmatches."abc"fails already inparseInt, so theNumberFormatExceptionblock runs and the second block is skipped.
catch (Exception e) goes last
Some problems you did not plan for still need a reply. Exception is the parent type of every exception in this module, so catch (Exception e) catches any of them; it must come after the specific catch blocks.
Example 2 · Specific first, Exception last
public class PickSubject { public static void main(String[] args) { int[] marks = {78, 85, 62}; String[] picks = {"1", "7", "x"}; for (String p : picks) { try { int i = Integer.parseInt(p); System.out.println("Mark: " + marks[i]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("No subject " + p); } catch (Exception e) { System.out.println("Other problem: " + e.getMessage()); } } }}Mark: 85 No subject 7 Other problem: For input string: "x"
What this code does
- Index 7 matches the first catch block, so the general one is skipped.
"x"throwsNumberFormatException. No specific block was written for it, socatch (Exception e)takes it.
In the other order, javac refuses: exception ArrayIndexOutOfBoundsException has already been caught. The general block would always match first, so the specific one below it could never run.
One catch for two types
Sometimes two problems need the same reply, like "bad number of people". Join the types with |, which means "or": catch (NumberFormatException | ArithmeticException e) is called a multi-catch.
Example 3 · Multi-catch with |
public class MultiCatch { public static void main(String[] args) { String[] inputs = {"4", "0", "four"}; for (String text : inputs) { try { int people = Integer.parseInt(text); System.out.println("Each gets " + 20 / people + " laddoos"); } catch (NumberFormatException | ArithmeticException e) { System.out.println("Bad number of people: " + text); } } }}Each gets 5 laddoos Bad number of people: 0 Bad number of people: four
What this code does
"0"throwsArithmeticExceptionand"four"throwsNumberFormatException; one block handles both.- Writing the same reply twice in two blocks would work too, but the multi-catch says it once.
The types in a multi-catch must not be parent and child. catch (NumberFormatException | Exception e) does not compile, because Exception already covers NumberFormatException.
Find the bug
Spot the bug
Select the line that is wrong.
This should print No such subject, but it does not compile.
Round 1 of 3catch (Exception e) everywhere: specific catch blocks let each problem get its own clear message.Next step
Check your understanding
9 quick questions