I'm using an API that wraps all the exceptions into one custom exception. So far so good. However, I want to rethrow the wrapped exceptions so my code can handle them separately. One approach would be:
catch (APIException e) {
Throwable cause = e.getCause();
if (cause instanceof MyException1) {
throw (MyException1)cause;
}
else if (cause instanceof MyException2) {
throw (MyException2)cause;
}
}
Is there a way to do this more intelligently? I was trying something with Generics, but couldn't find the right syntax. Somehting like:
catch (APIException e) {
Throwable cause = e.getCause();
Class<? extends Throwable> exceptionClass = cause.getClass();
throw exceptionClass.cast(cause); //doesn't quite work
}
Edited by: andrers2b on Nov 9, 2009 3:39 PM
Edited by: andrers2b on Nov 9, 2009 3:41 PM