Bird
0
0

Consider this controller code snippet:

medium📝 Debug Q14 of 15
Spring Boot - Exception Handling
Consider this controller code snippet:
@RestController
public class DemoController {
  @GetMapping("/demo")
  public String demo() {
    throw new IllegalArgumentException("Invalid argument");
  }

  @ExceptionHandler(NullPointerException.class)
  public String handleNull() {
    return "Null error";
  }
}

What will happen when the /demo endpoint is called?
AThe exception will be handled by handleNull method
BThe response will be "Null error" with HTTP 200
CThe exception will not be handled and a 500 error is returned
DThe server will return HTTP 404 Not Found
Step-by-Step Solution
Solution:
  1. Step 1: Identify the thrown exception

    The method throws IllegalArgumentException, but the handler only catches NullPointerException.
  2. Step 2: Check if exception is handled

    Since no handler matches IllegalArgumentException, Spring Boot returns a default 500 Internal Server Error.
  3. Final Answer:

    The exception will not be handled and a 500 error is returned -> Option C
  4. Quick Check:

    Handler mismatch causes 500 error [OK]
Quick Trick: Handler must match exception type exactly [OK]
Common Mistakes:
  • Assuming handler catches all exceptions
  • Thinking handler method runs for different exception types
  • Expecting 404 instead of 500 error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes