8

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
  • 24,791
  • 31
  • 112
  • 278
  • 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

22

From Java 7 it is possible :

try {
    //do some
} catch (NumberFormatException | ClassCastException | IllegaleArgumentException e) {
    return DynamicFilterErrorCode.INVALID_VALUE;
} 
Eran
  • 374,785
  • 51
  • 663
  • 734