Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple different types of exception handling in one catch block?

Tags:

java

If Multiple exceptions are allowed in catch() then it will reduce amount of redundant error handling code. for example,

try{
// some statments 
}
catch(Type1Exception t1, Type2Exception t2, Type3Exception t3) {   // wish if this could be allowed
/* t1, t2, t3 are children of Exception and needs same error handling then why to have different catch blocks with same piece of code */
}
like image 881
Delta Avatar asked Aug 16 '11 20:08

Delta


1 Answers

Yes - that's why it's supported in Java 7.

So your example would actually be:

try {
} catch (Type1Exception | Type2Exception | Type3Exception ex) {
   ...
}
like image 117
Jon Skeet Avatar answered Nov 09 '22 18:11

Jon Skeet