I'm working on my DAO and can't figure out what's the best way to handle exceptions. While using the .persist() there are 3 exceptions that can be emitted : EntityExistsException / IllegalArgumentException / TransactionRequiredException.
I'm just wondering what's the best way to catch and throw the exception (I want to handle it on a higher level).
Should I catch and throw a simple exception or is it more efficient to catch the above exceptions separately ?
First method, I just catch Exceptions and throw it:
public void addAccount(final Account accountToAdd) throws AccountJpaException {
try {
em.persist(accountToAdd);
} catch (Exception e) {
throw new AccountJpaException(e);
}
}
}
Second method : I catch every one of them separately
public void addAccount(final Account accountToAdd) throws AccountJpaException, AccountExistsException {
try {
em.persist(accountToAdd);
} catch (EntityExistsException e) {
throw new AccountExistsException(e);
}catch(IllegalArgumentException e){
throw new AccountJpaException(e);
}catch(TransactionRequiredException e){
throw new AccountJpaException(e);
}
}
}
Thank you for your advices!