Exceptions

In this lesson, we'll implement the exceptions needed for updating events in our ticket platform. We'll create custom exceptions to handle various error scenarios that could occur during event updates, making our application more robust and user-friendly.

Custom Exceptions

Let's create three custom exceptions that extend our base EventTicketException class:

public class EventNotFoundException extends EventTicketException { public EventNotFoundException() { } public EventNotFoundException(String message) { super(message); } public EventNotFoundException(String message, Throwable cause) { super(message, cause); } public EventNotFoundException(Throwable cause) { super(cause); } public EventNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
public class TicketTypeNotFoundException extends EventTicketException { public TicketTypeNotFoundException() { } public TicketTypeNotFoundException(String message) { super(message); } public TicketTypeNotFoundException(String message, Throwable cause) { super(message, cause); } public TicketTypeNotFoundException(Throwable cause) { super(cause); } public TicketTypeNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
public class EventUpdateException extends EventTicketException { public EventUpdateException() { } public EventUpdateException(String message) { super(message); } public EventUpdateException(String message, Throwable cause) { super(message, cause); } public EventUpdateException(Throwable cause) { super(cause); } public EventUpdateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }

Each exception serves a specific purpose:

  • EventNotFoundException - When a requested event doesn't exist
  • TicketTypeNotFoundException - When a referenced ticket type can't be found
  • EventUpdateException - For general update-related errors

Exception Handler

We'll add these exceptions to our global exception handler to ensure consistent error responses:

@RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(EventUpdateException.class) public ResponseEntity<ErrorDto> handleEventUpdateException(EventUpdateException ex) { log.error("Caught EventUpdateException", ex); ErrorDto errorDto = new ErrorDto(); errorDto.setError("Unable to update event"); return new ResponseEntity<>(errorDto, HttpStatus.BAD_REQUEST); } @ExceptionHandler(TicketTypeNotFoundException.class) public ResponseEntity<ErrorDto> handleTicketTypeNotFoundException(TicketTypeNotFoundException ex) { log.error("Caught TicketTypeNotFoundException", ex); ErrorDto errorDto = new ErrorDto(); errorDto.setError("Ticket type not found"); return new ResponseEntity<>(errorDto, HttpStatus.BAD_REQUEST); } @ExceptionHandler(EventNotFoundException.class) public ResponseEntity<ErrorDto> handleEventNotFoundException(EventNotFoundException ex) { log.error("Caught EventNotFoundException", ex); ErrorDto errorDto = new ErrorDto(); errorDto.setError("Event not found"); return new ResponseEntity<>(errorDto, HttpStatus.BAD_REQUEST); } // Other handler methods }

All these exceptions return HTTP 400 Bad Request responses, as they represent client-side errors.

Summary

  • Added the EventNotFoundException exception
  • Added the EventUpdateException exception
  • Added the TicketTypeNotFoundException exception
  • Updated the GlobalExceptionHandler to handle the new exceptions
© 2026 Devtiro Ltd. All rights reserved