Reputation: 317
I have written following code for exception handling of EJB.
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 catch block Exception object ee get me as null. What is an issue that it give me null value
Upvotes: 4
Views: 331
Reputation: 20691
You're not using the recommended method to retrieve the root-cause exceptions from an EJB. EJBException#getCausedBy()
is the recommended method for getting the root exception on an EJB.
According to the API however, it's even better if you get the cause from Throwable#getCause
. So what you should have is
Exception ee= ((Throwable)e).getCause();
Upvotes: 2