We need take more care when we work with reflections because we can find the exceptions which we normally find in a compilation time. With reflections you are manipulating classes and objects, thus it can be happening in execution time .
Below are the some common exceptions which we can have :
- NoSuchFieldException :
Doesn’t have a field of a specified name.
Thrown when a particular method cannot be found.
Thrown to indicate that a method has been passed an illegal or inappropriate argument. For example, in this case you can treat converting the argument type or adding a missed value, among others treatments.
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
It is the case which we really need speak about. Why?
This situation happen when the method or the constructor throw an exception. You will receive it, but the real exception is inside of it. It’s like the real exception is encapsulated inside the InvocationTargetException .
Use it for take the real exception : ex.getTargetException()
If you look the real exception was thrown for the MethodA and is a RuntimeException.
Now, you can see this 😛