Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Spring Boot - Exception Handling
Consider this code:
@ExceptionHandler({IOException.class, SQLException.class})
public ResponseEntity handleIOAndSQL(Exception ex) {
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Database or IO error");
}

What will happen if a FileNotFoundException is thrown?
AThe handler will catch it because FileNotFoundException is a subclass of IOException.
BThe handler will not catch it because FileNotFoundException is not listed explicitly.
CThe application will crash with unhandled exception.
DThe handler will catch it only if it is thrown from the same controller.
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception inheritance

    FileNotFoundException extends IOException, so it matches IOException.class in the handler.
  2. Step 2: Confirm handler catches subclasses

    @ExceptionHandler catches exceptions of the specified types and their subclasses.
  3. Final Answer:

    The handler will catch it because FileNotFoundException is a subclass of IOException. -> Option A
  4. Quick Check:

    @ExceptionHandler catches subclasses of listed exceptions [OK]
Quick Trick: Exception handlers catch subclasses of specified exceptions [OK]
Common Mistakes:
  • Thinking only exact classes are caught
  • Assuming unlisted subclasses are ignored
  • Believing handler scope affects catching subclasses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes