title: Catch all Exceptions and also return custom Errors in Jersey
body: <p>I want to catch all unexpected Exceptions in a jersey rest service.
  Therefore i wrote an ExceptionMapper:</p>
  
  <pre><code>@Provider
  public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper&lt;Exception&gt; {
      private static Logger logger = LogManager.getLogManager().getLogger(ExceptionMapper.class.getName());
  
      @Override
      public Response toResponse(Exception e) {
          logger.log(Level.SEVERE, e.getMessage(), e);
  
          return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal error").type("text/plain").build();
      }
  }
  </code></pre>
  
  <p>The mapper catches really all exceptions. Therefore i can't write:</p>
  
  <pre><code>public MyResult getById(@PathParam("id")) {
      if (checkAnyThing) {
          return new MyResult();
      }
      else {
          throw new WebApplicationException(Response.Status.NOT_FOUND);
      }
  }
  </code></pre>
  
  <p>This is catched by the Mapper. Now i have to write:</p>
  
  <pre><code>public Response getById(@PathParam("id") {
      if (checkAnyThing) { {
          return Response.ok().entity(new MyResult()).build();
      }
      else {
          return Response.status(Response.Status.NOT_FOUND).build();
      }
  }
  </code></pre>
  
  <p>Is this the correct way to catch all unexpected exceptions and also return errors (error codes) in jersey? Or is there any other (more correct) way?</p>