I wrote this little helper method to search the exception chain for a particular exception (either equals or super class). However, this seems like a solution to a common problem, so was thinking it must already exist somewhere, possibly in a library I have already imported. So, any ideas on if/where this might exist?
boolean exceptionSearch(Exception base, Class<?> search) {
Throwable e = base;
do {
if (search.isAssignableFrom(e.getClass())) {
return true;
}
} while ((e = e.getCause()) != null);
return false;
}
Take a look into Google Guava project. They have quite a few handy classes including one for exceptions. Eg, functionality you’ve just requested could be implemented in the next way:
Source code for this class: Throwables
Enjoy!