How are exceptions handled in EJB?

I wrote the following code to handle EJB exceptions.

1) Module.java

`@WebService(serviceName = "Module")
@Stateless()
public class Module {
    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public int hello(@WebParam(name = "name") String txt) throws Exception {
        int re=0;
        try{
            re=(6/0);     
        }catch(Exception e){
            throw (EJBException) new EJBException(se).initCause(e);
        }
        return re;;
        }
    }
}`

2) Client.jsp

`<%
try{
    selec.Module_Service service = new selec.Module_Service();
    selec.Module port = service.getModulePort();
    java.lang.String name = "";
    int result = port.hello(name);
    out.println("Result = "+result);
}catch(EJBException e){
    Exception ee=(Exception)e.getCause();
    if(ee.getClass().getName().equals("Exception")){
       System.out.println("Database error: "+ e.getMessage());
    }
}
%>`

Inside the catch block, the Exception ee object gets me as null. What is the problem that gives me a null value

+4
source share
1 answer

You do not use the recommended method to extract exceptions from the root cause from EJB. EJBException#getCausedBy()is the recommended method of getting root exceptions in EJB.

According to the API, however, it is even better if you get a reason from Throwable#getCause. So you must have

Exception ee= ((Throwable)e).getCause();
+2
source

All Articles