7

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)

2
  • 1
    Can you give move details, for example the class definition? Commented Apr 19, 2009 at 8:30
  • more instead of move of course Commented Apr 19, 2009 at 8:32

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So: always look at the full stack trace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.