Monday, June 13, 2011

Handling more than one exception, Java SE 7 new feature.

In Java SE 7 and later, a single catch block can handle 
more than one type of exception. This feature can reduce code 
duplication and lessen the temptation to catch an overly broad 
exception. 
 
consider the following example... 
try{
.............

}
catch (IOException ex) {
     logger.log(ex);
     throw ex;
catch (SQLException ex) {
     logger.log(ex);
     throw ex;
}
 
 
The following example, which is valid in Java SE 7 and later, 
eliminates the duplicated code:
 
catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
} 

Search Ranjeet's Blog