Tutorial :IllegalArgumentException: object is not an instance of declaring class


How is it possible that the "foo" exception is not thrown, but a subsequent call of invoke() throws the below exception?

if (method.getDeclaringClass() != object.getClass())      throw new RuntimeException("foo");    method.invoke(object);  

Thrown exception:

java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Beh... The method.invoke() call wasn't the one throwing directly. The target method was using invoke too and it threw, so it bubbled up.

Lesson learned: Handle InvocationTargetException separately from other exceptions.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »