Task Not Found Exception

What should we do when a user tries to update a task that doesn't exist?

This is certainly an error case, so let's create a custom exception to model this error.

Create the Task Not Found Exception

To keep things organised we'll create a dedicated package for our exceptions, com.devtiro.task.exception.

In this new package we'll create the TaskNotFoundException:

/** Thrown when a task is not found. */ public class TaskNotFoundException extends RuntimeException { /** The error message template. */ public static final String ERROR_MESSAGE = "Task with ID '%s' does not exist"; /** The ID of the task that was not found. */ private final UUID id; /** * Constructs a new TaskNotFoundException using * the ID of the task not found. * * @param id The ID of the task not found. */ public TaskNotFoundException(UUID id) { super(String.format(ERROR_MESSAGE, id)); this.id = id; } public UUID getId() { return id; } }

This exception has a single constructor that expects the ID of the task that wasn't found. It uses this ID to create a custom error message that it passes to the superclass constructor.

This way we can capture the ID and get a helpful error message in the stack trace.

Now we can move on to the business logic.

Summary

  • Created the TaskNotFoundException class.
© 2026 Devtiro Ltd. All rights reserved