Junior
Can there be multiple catch blocks in a single try block?
sobes.tech AI
Answer from AI
Yes, they can. For handling different types of exceptions.
try {
// Code that may generate exceptions
} catch (IOException e) {
// Handling input-output exceptions
} catch (SQLException e) {
// Handling database operation exceptions
} catch (Exception e) {
// Handling any other exceptions (general catch-all)
} finally {
// Code that always executes, regardless of exceptions
}
Exceptions in catch blocks are handled in the order they are specified. More specific exception types should come before more general ones.