Junior — Middle
Can you catch multiple error types in a single exception handling block?
sobes.tech AI
Answer from AI
Yes, in Java from version 7 onwards, you can handle multiple exception types in a single catch block by listing them separated by the vertical bar |.
Example:
try {
// code that may throw different exceptions
} catch (IOException | SQLException ex) {
// handling IOException and SQLException
ex.printStackTrace();
}
This is convenient when the same handling logic is required for different exceptions.