Reporting errors using application exceptions
It is recommended to use a checked application exception for recoverable error scenarios. In this section, we will see how a checked exception can be used in a RESTful web API implementation.
Here is a checked business exception definition for use in the JAX-RS resource method:
//Business exception class
public class DeprtmentNotFoundBusinessException extends Exception{
public DeprtmentNotFoundBusinessException(String message) {
super(message);
}
public DeprtmentNotFoundBusinessException(String message,
Throwable cause) {
super(message, cause);
}
//Rest of the implementation code goes here
}
The following code snippet uses DeprtmentNotFoundBusinessException
for reporting the DepartmentNotFound
error to the caller:
@DELETE @Path("departments/{id}") public void remove(@PathParam("id") Short id) throws DeprtmentNotFoundBusinessException { //Read department from data store for...