Junior — Middle
Is it possible to use multiple exception handlers for a single try block in a program?
sobes.tech AI
Answer from AI
Yes, in Java, you can use multiple exception handlers for a single try block. This is implemented using multiple catch blocks, each handling a specific type of exception.
Example:
try {
// code that may throw exceptions
} catch (IOException e) {
// handle IOException
} catch (SQLException e) {
// handle SQLException
} catch (Exception e) {
// handle all other exceptions
}
It is important that catch blocks are checked from top to bottom, so more specific exceptions should come before more general ones to avoid compilation errors.