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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With