According to Oracle documentation, the noteworthy points for the new block with several traps are:
catch (IOException|SQLException ex) { logger.log(ex); throw ex; }
If the catch block handles more than one type of exception, then the catch parameter is implicitly final . In this example, the catch ex parameter is final, and therefore you cannot assign any values ββto it in the catch block. A bytecode generated by compiling a catch block that handles several types of exceptions will be less (and therefore superior) than compiling many catch blocks that handle only one type of exception each. The catch block, which handles several types of exceptions, does not duplicate the bytecode generated by the compiler; bytecode does not have exception handler replication.
If exceptions can be handled differently, I suggest that you should catch them separately. If exception handling is the same for multiple exceptions, you can use a block with multiple traps.
try{//whatever} catch(A ex){//do something specific for A} catch(B ex){//do something specific for B} try{//whatever} catch(C ex){ //C is superclass of A and B and you are not concerned about the specific type // will catch even other exceptions which are instanceof C } try{//whatever} catch(A|B ex){//do the same thing for both A and B}
source share