There are more types of exceptions:
- System Exceptions (RuntimeExceptions, e.g. NullPointerException)
- Business exceptions (your own exception, an Exception extension, but not a RuntimeException, such as NotEnoughMoneyOnYourAccountException)
- Errors (e.g. OutOfMemoryError)
Usually you should catch business exceptions. But, of course, you can throw it to the client side if you want to process it there. By default, an EJB container does not roll back a transaction if you throw a business exception, but you can change this behavior by annotating your exception as follows:
@ApplicationException(rollback = true) public class NotEnoughMoneyOnYourAccountException extends Exception {
If your program throws a RuntimeException, it will be sent to the client terminated as a RemoteException, and your transaction will be canceled. They are less excluded than business exceptions, so we usually don’t catch them on the side of EJB.
Errors are least eliminated, they can even disable the JVM, usually we will not catch them, because usually we cannot process them in the program.
Donato Szilagyi
source share