Can I catch multiple Java exceptions in the same catch clause?

In Java, I want to do something like this: try { … } catch (/* code to catch IllegalArgumentException, SecurityException, IllegalAccessException, and NoSuchFieldException at the same time */) { someCode(); } …instead of: try { … } catch (IllegalArgumentException e) { someCode(); } catch (SecurityException e) { someCode(); } catch (IllegalAccessException e) { someCode(); } … Read more

Can I catch multiple Java exceptions in the same catch clause?

This has been possible since Java 7. The syntax for a multi-catch block is: try { … } catch (IllegalArgumentException | SecurityException | IllegalAccessException | NoSuchFieldException e) { someCode(); } Remember, though, that if all the exceptions belong to the same class hierarchy, you can simply catch that base exception type. Also note that you cannot … Read more