Java IllegalArgumentException:对象不是声明类的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/765052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:12:26  来源:igfitidea点击:

IllegalArgumentException: object is not an instance of declaring class

javareflection

提问by Jaka Jan?ar

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

怎么可能没有抛出“foo”异常,但随后调用 invoke() 会抛出以下异常?

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)

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

采纳答案by Jaka Jan?ar

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

Beh...method.invoke()电话不是直接扔的。目标法也用invoke了,扔了,就冒泡了。

Lesson learned: Handle InvocationTargetExceptionseparately from other exceptions.

经验教训:InvocationTargetException与其他异常分开处理。