I've got a callback with exception instance. Currently i handle it this way, but i think that there is a better way. Would like to hear some comment from Java expert. =)
...
onError(Exception e) {
if (e instanceof IOException) {
ioe = (IOException)e;
// do smth with ioe
} else if (e instanceof MyException) {
mye = (MyException)e;
// do smth with mye
}
}
...
I'm not 100% sure about what you mean by "handle exceptions inside callback", but the onError
method you provided could be better expressed like this:
...
onError(Exception e) {
try {
throw e;
} catch (IOException ioe) {
// do smth with ioe
} catch (MyException mye) {
// do smth with mye
}
}
...
I think you could override "onError" with subclasses of exceptions:
interface MyExceptionHandler
{
onError(Exception e) {
// Default exception
}
onError(IOException ioe) {
// do smth with ioe
}
onError(MyException mye) {
// do smth with mye
}
}
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