I have a stateless session bean with a method which throws an exception (which extends from Exception).
@Stateless
@LocalBean
public class MyBean {
...
public void myMethod() throws MyException {
...
}
}
But I am not able to catch the exception.
try {
myBean.myMethod
} catch (MyException e) {
...
}
Here is stack:
WARNING: StandardWrapperValve[MyServlet]: PWC1406: Servlet.service() for servlet MyServlet threw exception
java.lang.IllegalAccessError: example/MyException
at $Proxy327.myMethod(Unknown Source)
at example.EJB31_Generated_MyBean_Intf_Bean_.myMethod(Unknown Source)
at example.MyServlet.processRequest(MyServlet.java:36)
at example.MyServlet.doGet(MyServlet.java:71)
...
I think, it’s because calling the bean method goes through proxy. How can I catch the exception? Or methods throwing exceptions in a session bean should be avoided? – I hope not.
as the stacktrace shows it’s actually an IllegalAccessError which is thrown… see its API doc: http://docs.oracle.com/javase/6/docs/api/java/lang/IllegalAccessError.html
you wont be able to catch this by adding
catch (Exception e) {...}… as it’s an Error, which is not a subclass of Exception. If you REALLY want to catch it, you would have to add acatch (Throwable t) {...}which I highly discourage. Rather find the reason for the error being thrown. I think we need to see more of your code…As the API says “Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed”… did you redeploy on server and client to be sure to use the same interface definition?