IllegalArgumentException:object不是声明类的实例

时间:2009-04-19 08:08:55

标签: java reflection

“foo”异常怎么可能抛出,但后续调用invoke()会引发以下异常?

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

method.invoke(object);

抛出异常:

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

1 个答案:

答案 0 :(得分:13)

呃...... method.invoke()电话不是直接投掷的。目标方法也使用了invoke并且它扔了,所以它冒出来了。

获得的经验:将InvocationTargetException与其他例外分开处理。

相关问题