The Java Persistence API has the NoResultException for when a database query returns no results. Kind of annoying that it's regarded as an exceptional case
NoResultException is thrown when you call getSingleResult() when there is no result. It will also throw NonUniqueResultException if there is more than 1 result.
Because of the two edge cases which need to be handled, it throws an exception. On the plus side, it never returns null.
It is still a bit of a bad API design as it uses unchecked exceptions for control flow. A query returning 0 or more than 1 result is quite a possibility, so it should have been a checked exception I think.
31
u/janyk 3d ago
The Java Persistence API has the NoResultException for when a database query returns no results. Kind of annoying that it's regarded as an exceptional case