Unchecked Exceptions

On our new project at work we’re using JPA sitting on top of Hibernate.

I’ve used Hibernate several times now and am familiar with it.

JPA is mostly similar in use but there are a few gotchas.

One that got me the other day was what happens when you write a query that you expect to return a single result.

In Hibernate I’d have called query.uniqueResult();

The Javadoc for that method says:

Convenience method to return a single instance that matches the query, or null if the query returns no results.

So, the query either returns my object or null (an exception is thrown if my query returns more than one result – fair enough).

I had to do something similar in JPA-land so I looked at its Query class.

It offered a similarly named method: query.getSingleResult();.

All good, I wrote my code, compiled it and restarted my application server.

Unfortunately, when I ran the code, it fell over with a NoResultException.

For my particular query, there were no results in our test database.

Fine, my code can deal with that, but clearly the JPA method works quite differently from the Hibernate version.

Its Javadoc says:

Execute a SELECT query that returns a single result.


the result


NoResultException – if there is no result

So, unlike the Hibernate version this one will throw an exception if the query returns no results.

Hmmmm, I think I prefer the Hibernate version.

Of course, if it had thrown a checked exception my code would not have even compiled.

As it was it was just luck that the database had no results so I found the problem right away.

I’m not saying unchecked exceptions are bad, on the whole I prefer them.

But there’s a certain element of retraining your brain to no longer rely on the compiler to tell you that you’re dealing with all possible error conditions.

I know, I know, there wouldn’t be a problem if I’d read the Javadoc up front, but how many people can honestly say that they read the Javadoc for every new method the first time that they call it?

2 thoughts on “Unchecked Exceptions”

  1. It’s important for API designers to remember that Exceptions should be used for Exceptional circumstances – not as stand-ins for return values.

    So for this particular example I would say that it is pretty likely that such a query might return no results – and in this respect the Hibernate API is correct, and the JPA API is flawed (assuming that the method is intended to be just run on arbitrary queries, and not in specific circumstances where no results is truly exceptional).

  2. I agree with Joe – this is simply a flaw in the API. A query returning no results is not exceptional. A query that is supposed to return a single result that actually returns multiple results is exceptional – it indicates a possible data integrity or query problem – so in that situation, an exception is correct.

    As an aside; until recently I developed primarily using vi, only falling back to the IDE when I needed to debug. Switching fully to Eclipse is on the whole a good thing – but I find I spend much less time looking at Javadocs, and subsequently developing a deeper understanding of the API is more difficult (and as you’ve discovered, more painful).

Comments are closed.