Reputation: 2342
I have some custom exception that should be retrieved from thrown InvocationTargetException, I do it in following way:
try {
...
}
catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException e) {
if (e.getCause() instanceof CustomException) {
throw (CustomException) e.getCause();
}
throw new IllegalArgumentException();
}
But findbugs complains me:
Unchecked/unconfirmed cast from Throwable
I found a silimar question (how can resolve dodgy:unchecked/unconfirmed cast in sonar?), but it didn't help.
Upvotes: 2
Views: 4675
Reputation: 97202
I think that if you assign the cause to a local variable first, FindBugs will be able to figure things out correctly:
try {
/* ... */
} catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException e) {
Throwable cause = e.getCause();
if (cause instanceof CustomException) {
throw (CustomException) cause;
}
throw new IllegalArgumentException();
}
Upvotes: 6