Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB wraps all Exceptions into EJBException

Tags:

java

jboss

ejb

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?

like image 444
Mr. P Avatar asked Jan 13 '15 13:01

Mr. P


1 Answers

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.

like image 132
slwk Avatar answered Nov 07 '22 10:11

slwk