12

I have the following code:

try {
    //do some
} catch (NumberFormatException e) {
    return DynamicFilterErrorCode.INVALID_VALUE;
} catch (ClassCastException e) {
    return DynamicFilterErrorCode.INVALID_VALUE;
} catch (IllegaleArgumentException e) {
    return DynamicFilterErrorCode.INVALID_VALUE;
}

Is it possible to combine those 3 catch clauses into one? They have exactly the same handler code, so I'd like to reuse it.

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • From java 7 only it is possible. Till java 6, you may handle by catching common parent exception class. However, it will include all other child of that exception. – Panther May 14 '15 at 10:23

1 Answers1

26

From Java 7 it is possible :

try {
    //do some
} catch (NumberFormatException | ClassCastException | IllegaleArgumentException e) {
    return DynamicFilterErrorCode.INVALID_VALUE;
} 
Eran
  • 387,369
  • 54
  • 702
  • 768