如何不抛出"foo"异常,但后续调用invoke()会抛出以下异常?
if (method.getDeclaringClass() != object.getClass())
throw new RuntimeException("foo");
method.invoke(object);
Run Code Online (Sandbox Code Playgroud)
抛出异常:
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Jak*_*čar 13
呃......这个method.invoke()
电话不是直接投掷的.目标方法也在使用invoke
它扔了,所以它冒出来了.
获得的经验教训:InvocationTargetException
与其他例外情况分开处理.