UnsuppotedOperationException.getCause returns itself
I have the following Java code in Android
Method getIfaceMethod =
service.getClass().getDeclaredMethod("getIface", new Class<?>[0]);
getIfaceMethod.invoke(param1)));
Sometimes, a failure to call a method through reflection throws an exception. The exception thrown is UnspportedOperationException. Oddly enough, when I call getCause, it returns itself – look at the screenshot of the debugger:
What the heck??
Solution
If you look at sourcecode throwable
Then you see that the default value for the variable cause
is this, which means that the cause of this
exception has not been initialized
If you < a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Throwable.java#Throwable.getCause%28%29" rel="noreferrer noopener nofollow"> further lookIn Throwable's
getCause()
method, you’ll see that if cause
still points to itself, you should actually check it and return null.
However, you are using the debugger to examine the fields of the exception (!) reason
, so this check is not performed.