If you have:
catch (FooException ex) {
throw new BarException (ex);
}
catch (BarException ex) {
System.out.println("hi");
}
…and the first catch clause is triggered (i.e. FooExcepetion has occurred),
does the new BarException get immediately caught by the subsequent “catch” clause?
Or is the new BarException thrown one level up the continuation stack?
I realize this is a basic question. : )
It does not get caught by the 2nd catch clause, no.
Each catch clause in the list is tried, to see if it matches. The first one that matches is the only one that runs, then the code moves on to the
finallyclause.Another result of this is that if you have:
then block 1 is the only one that will run, even though block 2 would have matched. Only the first matching catch clause is evaluated.