EJB wraps all exceptions in EJBException

Assuming I have @Stateless bean :

@Local
public interface A {
    public void check() throws MyException {
}

@Stateless
public class AImpl implements A {
    public void check() throws MyException {
        ...
        try {
            data = getDataFromService();
        } catch (RException e) {   
            throw new MyException(e);
        }
    }
}

      

Exceptions:

public class MyException extends Exception{}
public class RException extends RuntimeException{}

      

When I inject this bean into some other class using @EJB annotation and execute check () method, I get EJBException with MyException as reason ...

public class B {
    @EJB private A a;
    public void start() {
        try {
            a.check();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
}

      

How can I get this to throw the correct exception?

Any ideas how to get this to work well?

Is there a way to make it work without catching the EJBException and throwing the cause of the exception?

+4


source to share


1 answer


I am assuming your MyException

extends RuntimeException

and hence this is a thrown exception. in this case, you can annotate your exception with annotation @ApplicationException

. As a result, the exception will be an application exception rather than a system exception. When a system exception is thrown, it is wrapped in an EJBException, but application exceptions are passed directly to the client.



+5


source







All Articles