Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 979
Jaka Jančar Avatar asked Apr 19 '09 08:04

Jaka Jančar


1 Answers

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.

like image 157
Jaka Jančar Avatar answered Sep 22 '22 04:09

Jaka Jančar