users@jersey.java.net

Re: [Jersey] exception handling best practices

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Tue, 23 Feb 2010 14:41:03 -0800

On Mon, Feb 22, 2010 at 12:02 PM, Craig McClanahan <craigmcc_at_gmail.com> wrote:
...
> My favorite approach is to create exception classes (extending
> RuntimeException or WebApplicationException) that correspond to
> various error conditions, then use ExceptionMapper to map them to an
> appropriate status code with an appropriately formatted message (with
> an appropriate media type).  With these, I can tell a developer
> writing a GET method in a resource class, that when the user tries to
> access a resource that doesn't exist, just "throw
> NotFoundException(...)" and let Jersey take care of remembering that
> this should be a 404 with the correct media type and message format.

For what it's worth, that is similar to what I like to do. There's
limited number of exception types that are commonly needed, so
overhead tends to grow logarithmically; at first it may seem like lots
of extra work, but not so much for the long run. And it does decouple
things reasonably well.
But I agree in that there's more than one way to skin the cat,

-+ Tatu +-