Java Reference
In-Depth Information
Creating MyResource. Level = 2
Using MyResource level 2
Using MyResource level 1
Low in level.
Suppressed exception messages are...
Error in closing
A Multi-Catch Block
Java 7 added support for a multi-catch block to handle multiple types of exceptions in a catch block. Suppose you want
to catch three exceptions: Exception1 , Exception2 , and Exception3 . Prior to Java 7, your code would look as follows:
try {
// Code that may throw Exception1, Exception2, or Exception3
}
catch (Exception1 e1){
// Handle Exception1
}
catch (Exception2 e2){
// Handle Exception2
}
catch (Exception3 e3){
// Handle Exception3
}
Prior to Java 7, each exception must be handled in a separate catch block. This sometimes resulted in code
duplication when multiple exceptions were handled in the same way. Sometimes, instead of using a separate catch
block to catch multiple exceptions, a programmer would use one catch block and specify the parameter of a more
generic exception type.
try {
// Code that may throw Exception1, Exception2 or Exception3
}
catch (Throwable t){
// Handle any exception
}
Java 7 addresses both issues:
The deficiency in Java language that did not let a programmer handle multiple exceptions in
one catch block
The laziness of programmers of using a generic exception type to handle multiple exceptions
in a catch block
 
Search WWH ::




Custom Search