United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6626834 : Exception handling : catch multiple exceptions in single catch clause

Details
Type:
Enhancement
Submit Date:
2007-11-07
Status:
Closed
Updated Date:
2010-04-04
Project Name:
JDK
Resolved Date:
2007-11-07
Component:
specification
OS:
windows_xp
Sub-Component:
language
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
7
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

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.

                                    

Comments



Hardware and Software, Engineered to Work Together