Java Reference
In-Depth Information
Display 9.8
Catching Multiple Exceptions (part 2 of 2)
34 }
35 System.out.println("End of program.");
36 }
37 }
Sample Dialogue 1
How many pencils do you have?
5
How many erasers do you have?
2
Each eraser must last through 2.5 pencils
End of program.
Sample Dialogue 2
How many pencils do you have?
−2
Cannot have a negative number of pencils
End of program.
Sample Dialogue 3
How many pencils do you have?
5
How many erasers do you have?
0
Do not make any mistakes.
End of program.
PITFALL: Catch the More Specific Exception First
When catching multiple exceptions, the order of the catch blocks can be important.
When an exception is thrown in a try block, the catch blocks are examined in
order, and the first one that matches the type of the exception thrown is the one that
is executed. Thus, the following ordering of catch blocks would not be good:
catch (Exception e)
{
.
.
.
}
(continued)
 
 
Search WWH ::




Custom Search