SCJP 1.4, SCWCD 1.3, SCBCD 1.3
'Make everything as simple as possible, but not simpler.' --Albert Einstein
Originally posted by Adrian Bates:
So in my test case where I am forcing it to throw an application exception, DuplicateKeyException, I can catch this and throw an EJBException instead and this will cause a rollback? Is that right?
Adrian
'Make everything as simple as possible, but not simpler.' --Albert Einstein
Throwing a RuntimeException is not a good practice.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Originally posted by Roger Chung-Wee:
What is you reason for saying this?
'Make everything as simple as possible, but not simpler.' --Albert Einstein
In my opinion all application exceptions (Business logic) exceptions should be checked exceptions.
Because, the client should be aware about the fact that the methods can throw an exception and be able to handle that exception.
In EJB if you throw a runtime exception the transaction will be rolled back, but maybe the business logic requires other handling...
For example:
In a method withdraw(500) if an InsufficientFundsException is thrown, you could withdraw as much as you can and go further. BUT if InsufficientFundsException is unchecked the transaction is already set for rollback....
Check that your code is not throwing a checked exception, eg SQLException. If so, wrap it in EJBException and throw this exception.
Check that your code is not throwing a checked exception which is not an application exception, eg SQLException. If so, wrap it in EJBException and throw this exception.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Consider Paul's rocket mass heater. |