views:

6294

answers:

1

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)

+4  A: 

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.

Jaka Jančar
hhaaa. I already have thought of that :)
bruno conde
Ouch ... that one's nasty!
Joachim Sauer
So: always look at the full stack trace.
Joachim Sauer