Senior
How to simplify processing if we want to handle multiple exceptions in the same way (Multi-catch)?
sobes.tech AI
Answer from AI
In Java, starting from version 7, it became possible to use multi-catch — a single catch block for multiple exceptions. This simplifies the code if the handling for different exceptions is the same.
Example:
try {
// code that may throw IOException or SQLException
} catch (IOException | SQLException ex) {
// common handling for IOException and SQLException
ex.printStackTrace();
}
Thus, there is no need to write separate catch blocks for each exception if the handling logic is the same.