-
Enhancement
-
Resolution: Duplicate
-
P5
-
None
-
5.0, 6
-
generic, x86
-
generic, windows_xp
A DESCRIPTION OF THE REQUEST :
Reflection can potentially throw many different kinds of exceptions. Suppose you use reflection to create a new object, set one of its fields, and call a method. You are now facing six different checked exceptions:
ClassNotFoundException
InstantiationException
NoSuchMethodException
NoSuchFieldException
IllegalAccessException
InvocationTargetException
The problem is that all of these exceptions simply extend Exception, so you have no choice but to catch and deal with them all individually. As the response to all five different exceptions is usually the same ("report an error"), the fact that we are forced to deal with them all individually is silly.
An easy fix would be to create a superclass of all of these exceptions, e.g. ReflectionException, which would enable us to catch all reflection exceptions in one shot. This would only take a few seconds to implement, maintain both source and binary compatibility, and would make reflection tremendously more convenient to use.
JUSTIFICATION :
Reflection is often used in situations where success is expected: i.e., the method, field, and class names come from a programmer-controller configuration file or similar 'safe' repository, so we know that they actually exist and the calls will succeed.
Having to catch checked exceptions in a situation where success is all but guaranteed is annoying to begin with; having to catch six different kinds of checked exceptions is downright painful. On top of that, in this particular case there is no reasonable response other than to report an error to the user, so one ends up with code that looks like:
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
catch (InstantiationException e) {
throw new RuntimeException(e);
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
catch (IllegalAcceessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
I see no disadvantage to being able to write:
catch (ReflectionException e) {
throw new RuntimeException(e);
}
instead.
Reflection can potentially throw many different kinds of exceptions. Suppose you use reflection to create a new object, set one of its fields, and call a method. You are now facing six different checked exceptions:
ClassNotFoundException
InstantiationException
NoSuchMethodException
NoSuchFieldException
IllegalAccessException
InvocationTargetException
The problem is that all of these exceptions simply extend Exception, so you have no choice but to catch and deal with them all individually. As the response to all five different exceptions is usually the same ("report an error"), the fact that we are forced to deal with them all individually is silly.
An easy fix would be to create a superclass of all of these exceptions, e.g. ReflectionException, which would enable us to catch all reflection exceptions in one shot. This would only take a few seconds to implement, maintain both source and binary compatibility, and would make reflection tremendously more convenient to use.
JUSTIFICATION :
Reflection is often used in situations where success is expected: i.e., the method, field, and class names come from a programmer-controller configuration file or similar 'safe' repository, so we know that they actually exist and the calls will succeed.
Having to catch checked exceptions in a situation where success is all but guaranteed is annoying to begin with; having to catch six different kinds of checked exceptions is downright painful. On top of that, in this particular case there is no reasonable response other than to report an error to the user, so one ends up with code that looks like:
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
catch (InstantiationException e) {
throw new RuntimeException(e);
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
catch (IllegalAcceessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
I see no disadvantage to being able to write:
catch (ReflectionException e) {
throw new RuntimeException(e);
}
instead.
- duplicates
-
JDK-6482910 (reflect) Define ReflectionException
- Closed
-
JDK-6857789 (reflect) Create common superclass of reflective exceptions
- Resolved