try { // execute code that may throw 1 of the 3 exceptions below. } catch(SQLException e) { logger.log(e); } catch(IOException e) { logger.log(e); } catch(Exception e) { logger.severe(e); }As you can see, the two exceptions
SQLException
and IOException
are handled in the same way, but you still have to write two individual catch
blocks for them.In Java 7 you can catch multiple exceptions using the multi catch syntax:
try {
// execute code that may throw 1 of the 3 exceptions below.
} catch(SQLException | IOException e) {
logger.log(e);
} catch(Exception e) {
logger.severe(e);
}
Notice how the two exception class names in the first catch
block
are separated by the pipe character |
. The pipe character between exception class names
is how you declare multiple exceptions to be caught by the same catch
clause.
@reference_1_tutorials.jenkov.com
Catching Multiple Exceptions in Java 7
No comments:
Post a Comment