JDK-6626834 : Exception handling : catch multiple exceptions in single catch clause
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-11-07
  • Updated: 2010-04-04
  • Resolved: 2007-11-07
Related Reports
Duplicate :  
Description
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.