I have an application with three-tire architecture. And I don't understand how to work with exceptions in this context. I collected some questions:
1 . Do I need to create a generic exceptions, like PersistentException
, and make all DAO classes methods to throw exeption only of one type - PersistentException
? I.e. inside every DAO method (CRUD) do like this:
public create(Entity instance) {
try {
...// all operations here
} catch(Exception e) {
throw new PersistentException(e);
}
}
2 . It's OK to create one exception class for every EJB service (One exception per EJB interface)?
I.e. suppose I have a EJB beans like PersonManagementBean
, OrganizationManagementBean
, EmployeeManagementBean
with corresponding @local
and @remote
interfaces. They are exposed to clients, i.e. in fact they are Session Facades (so they are located in service layer). So it's good idea to create corresponding Exception classes for every bean (PersonManagementException
,OrganizationManagementException
, EmployeeManagementException
)?
Or it's better to have only one exception called ServiceException
(as in case of DAOs)?
3 . What type of exceptions can throw my service (busyness) level (in common case)? Can I propagate DAO (PersistentException
) exceptions to clients? i.e.
public void relocatePerson() {
try {
Person p = personDao.getPerson(); // can throw PersistentException
....
if (someCondition) {
throw new PersonManagementException(); // throwing same PersonManagementException
}
....
} catch(PersonManagementException e) {
throw e; //don't need to rewrap same exception
} catch(PersistentException e) {
throw e; // DO I need to throw it as PersistentException to client? Or it's better to rewrap it as PersonManagementException?
} catch(Exception e) {
throw new PersonManagementException(e) //throwing all exception as service specific exception
}
}
Or I need to rethrow all exceptions (in common case) as service-specific exceptions?
- "In common case" I mean here that I know that in some cases some methods can throw additional exceptions with some helpful information (for example
ValidationException
with information about which objects don't pass validation rules)