pisek
pisek

Reputation: 1448

EJB wraps all Exceptions into EJBException

Assuming I have a @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 am injecting this bean to some other class with the use of @EJB annotation and executing the check() method, I am getting EJBException with MyException as its cause...

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

How can I make it throw proper exception?

Any ideas how to make it work ok?

Is there any way to make it work without intercepting the EJBException throw, and rethrowing its exception cause?

Upvotes: 5

Views: 3453

Answers (1)

slwk
slwk

Reputation: 607

I assume your MyExceptionextends RuntimeException and therefore it is unchecked exception. in such case you can annotate your exception with @ApplicationException annotation. As a result of that your exception will be an application exception instead of a system exception. When a system exception is thrown it is packaged in EJBException, but application exceptions are thrown directly to the client.

Upvotes: 9

Related Questions