2014-11-28 16 views
4

EJB의 예외 처리를 위해 다음 코드를 작성했습니다.EJB에서 예외가 처리되는 방법은 무엇입니까?

1)

`@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 Module.java

) 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()); 
    } 
} 
%>` 

내부 캐치 블록 예외 객체는 EE가 null로 저를 얻는다. null 값을주는 문제점은 무엇입니까

답변

2

권장되는 방법을 사용하여 EJB에서 근본 원인 예외를 검색하지 않습니다. EJBException#getCausedBy()은 EJB에서 루트 예외를 가져 오는 데 권장되는 방법입니다.

그러나 API에 따르면 Throwable#getCause에서 원인을 얻는 것이 더 좋습니다. 그래서 당신이해야 할 것은

Exception ee= ((Throwable)e).getCause(); 
+0

의 Throwable의 캐스트가 너무 너무 내 생각 –

+0

중복이지만, 어떤 이유로 문서는 어떻게 든 바람직하다고 판단. 나는 완벽을 위해서만 옵션을 추가했다. @SteveC – kolossus

관련 문제