2

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)
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286

2 Answers2

0

Yes, you should make all DAO classes methods to throw exeption only of one type - PersistentException. Because it may help you to catch every kind of DB related exceptions into 1 type. Moreover, you can set messages about the particular exception while setting it into PersistantException using parametrized constructor. i.e. throw new PersistentException("Exception while opening a connection",e);

Your second question totally depends on your requirement. If you want to show different errors and showing different error pages and want to handle them (errors of each bean) separately then you should create separate exception class for each of your beans.

Your third question, as per my point of view its fine. You can propagate PersistentException to the level from where DAO or Helpers are being called first - i.e. ActionBean OR servlet. There you can set your error messages and then you can throw them to your architecture level handlers (which is generally configured in configuration or xml files)

While working with exceptions dont forget that "to throw early and catch late"

Ravi A
  • 505
  • 1
  • 5
  • 16
0

For any exception that signals failure, use just one exception. Rationale: the client can't do anything in this case but log the stacktrace and/or report the error to the user.

I some special circumstances you'll need to throw an exception just to signal that an alternative approach to serving the request is needed. Only these cases need a specific exception.

The remote client will almost never want to know anything else than that a failure occurred; be very careful not to burden your remote interface with redundant exception classes.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • ok, thanks. But can you explain some more deeper this words: `be very careful not to burden your remote interface with redundant exception classes`. What if every of my remote method will be marked as `throws MyExecption`? What worng with it? And what type of exception must be thrown by EJB - checked or unchecked exceptions? Thanks – WelcomeTo Jan 18 '13 at 14:16
  • All the exceptions you declare will have to be included in the client-side JAR dependency. If they bring no business value, they are just dead weight. Since all EJB exceptions must extend `RemoteException`, you don't have a choice between checked and unchecked. – Marko Topolnik Jan 18 '13 at 14:19
  • Are you sure? I think it is not strong requirement to extend `RemoteException` (at least in EJB 3.0). I have experience with extending `Exception`, not `RemoteException`. – WelcomeTo Jan 18 '13 at 14:39
  • My knowledge is from EJB 2.1; I welcome such a change. I always prefer unchecked exceptions: when you need to catch an exception, you'll know it without the compiler telling you to. Even if you don't catch it, this is the easiest bug ever to solve. – Marko Topolnik Jan 18 '13 at 14:41