Re: Catching mulitple Exceptions in JDK 1.7
Jukka Lahtinen wrote:
Roedy Green writes:
catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}
Is it correct to say that I can't get at any of the parameters of the
IOException if I use the new JDK 1.7 multiple catch?
if (ex instanceof IOException) {
IOException ioe = (IOException) ex;
...
}
Of course, if you need something in the catch block that is specific to
IOException, you probably wouldn't do
catch (IOException|SQLException ex)
SQLException /is-an/ IOException, so there isn't anything "specific to IOEx=
ception" that isn't in SQLException. It's the other way around. Roedy was=
pointing out that if you use multi-catch on IO/SQL, that you can't get at =
the stuff that's specific to SQLException. But as you correctly point out,=
if you need to handle the two cases differently, you wouldn't combine them=
..
In fact, if you only want to handle the IOException-ness of the exception, =
you wouldn't even bother mentioning 'SQLException' at all. You'd just 'cat=
ch(IOException...)'.
--
Lew