Re: Why catch specific exceptions
"Frank Cisco" <tdyjkdftyujdtjyj@dtyjdtyjdtyjdty.com> wrote in message
news:9e_Yl.51077$2t7.35768@newsfe16.ams2...
Why not wrap everything around 'throws Throwable' and
'try{...}catch(Throwable t)' then deal with the error by catching it and
dealing with each case with instanceof?
Declaring everything throws Throwable simply says that something bad might
happen, but I'm not telling you what it is. You have to guess. It compiles,
but it isn't very helpful to the eventual consumers of your method.
As for catching Throwable and using instanceof to sort it all out, consider
this (13 lines)...
try {
....
} catch (Throwable t) {
if(t instanceof IOException) {
// cleanup I/O problem
} else if (t instanceof NullPointerException) {
// cleanup NPE problem
} else if (t instanceof IllegalArgumentException) {
// cleanup IAE problem
} else if (t instanceof Throwable) {
throw t; // can't handle it -- rethrow
}
}
....vs. this (nine lines)...
try {
....
} catch (IOException ioe) {
// cleanup I/O problem
} catch (NullPointerException npe) {
// cleanup NPE problem
} catch (IllegalArgumentException iae) {
// cleanup IAE problem
}
.... same logic.