Duplicate :
|
A DESCRIPTION OF THE REQUEST : A code snippet as following has few problems.' catch (IllegalArgumentException e ) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } Though for all of the catch clauses it does the same operation, we have to write mutiple catch clauses. Instead would it not be better to handle like the following : catch (IllegalArgumentException e || IllegalAccessException e1 ||(SecurityException e2 || NoSuchMethodException e3 || InvocationTargetException e4 || InstantiationException e5) { // do something } JUSTIFICATION : - Removes extra code - Becomes modular EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - catch (IllegalArgumentException e || IllegalAccessException e1 ||(SecurityException e2 || NoSuchMethodException e3 || InvocationTargetException e4 || InstantiationException e5) { // do something } ACTUAL - catch (IllegalArgumentException e ) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } ---------- BEGIN SOURCE ---------- catch (IllegalArgumentException e ) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } to be : catch (IllegalArgumentException e || IllegalAccessException e1 ||(SecurityException e2 || NoSuchMethodException e3 || InvocationTargetException e4 || InstantiationException e5) { // do something } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : yes. There are workarounds.