Spring Boot

Exception Handling

Exception handling with @ControllerAdvice, @ExceptionHandler, @ResponseStatus, error standardization

25 pertanyaan wawancara·
Mid-Level
1

What is @ControllerAdvice in Spring MVC?

Jawaban

@ControllerAdvice allows centralizing exception handling for all application controllers. This annotation creates a global component that intercepts exceptions thrown by any @Controller or @RestController. This avoids duplicating error handling logic in each controller and promotes error response consistency.

2

How to handle a specific exception in a controller?

Jawaban

@ExceptionHandler allows defining a method that handles one or more specific exceptions in a controller or @ControllerAdvice. The annotated method is automatically invoked when the specified exception is thrown. It's possible to return a ResponseEntity to control the HTTP status and response body.

3

What is the purpose of @ResponseStatus on an exception class?

Jawaban

@ResponseStatus on a custom exception allows automatically mapping this exception to a specific HTTP status code. When the exception is thrown, Spring automatically returns the indicated HTTP status without requiring an @ExceptionHandler. This approach simplifies code for simple exceptions that directly correspond to an HTTP status.

4

Which exception is thrown when validation with @Valid fails?

5

What is ProblemDetail in Spring 6+?

+22 pertanyaan wawancara

Kuasai Spring Boot untuk wawancara berikutnya

Akses semua pertanyaan, flashcards, tes teknis, latihan code review dan simulator wawancara.

Mulai gratis