2010-05-26 31 views
1

我在Java bean中拋出NullPointerException,並在FacesServletWrapper中捕獲異常。 FacesServletWrapper我總是隻獲得ServletException。MyFacesServlet包裝中的FacesMessages

  1. 我該如何捕獲我拋出的特定異常?

  2. 我該如何繼續從哪裏引發異常?

在我的豆:

public String getSize() { 
    try { 
    Object object = null; 
    object.equals(""); 

    } catch (Exception e) { 
    throw new NullPointerException(); 
    } 


} 

我的servlet:

public class FacesServletWrapper extends MyFacesServlet { 

public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES"; 
public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID"; 

private ServletConfig servletConfig; 
private FacesContextFactory facesContextFactory; 
private Lifecycle lifecycle; 

@Override 
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { 
    FacesContext facesContext = facesContextFactory.getFacesContext(servletConfig.getServletContext(), request, response, (javax.faces.lifecycle.Lifecycle) lifecycle); 
    try { 
    super.service(request, response); 
    } catch (Throwable e) { 
    Locale locale = (Locale) facesContext.getExternalContext().getSessionMap().get(Constants.LOCALE); 
    ServletContext context = servletConfig.getServletContext(); 
    RequestDispatcher dispatcher = context.getRequestDispatcher("/errors/error.jsf"); 

    if (e instanceof NullPointerException) { 
         //here I catch only ServletException 
    String error = ResourceUtil.getMessage("Login_failed", locale); 
    facesContext.getExternalContext().getSessionMap().put("error", error); 
    dispatcher.forward(request, response); 
    ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/errors/error.jsf"); 
    } 
    } 

} 

public void destroy() { 
    servletConfig = null; 
    facesContextFactory = null; 
    lifecycle = null; 
} 

public ServletConfig getServletConfig() { 
    return servletConfig; 
} 

private String getLifecycleId() { 
    String lifecycleId = servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR); 
    return lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE; 
} 

@Override 
public void init(ServletConfig servletConfig) throws ServletException { 
    super.init(servletConfig); 
    this.servletConfig = servletConfig; 
    facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); 
    LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
    lifecycle = (Lifecycle) lifecycleFactory.getLifecycle(getLifecycleId()); 
} 
} 

謝謝!

+0

你能提供一個堆棧跟蹤,以便我們可以看到它被拋出的位置嗎? – FrustratedWithFormsDesigner 2010-05-26 15:34:55

回答

0

你打電話FacesServlet#service()這裏:

try { 
    super.service(request, response); 
} catch (Throwable e) { 
    // ... 
} 

下面是its javadoc提取學習它會拋出什麼樣的異常:

如果FacesException在任何情況下被拋出,解壓原因來自FacesException。如果原因是nullFacesException中提取消息,請將其放入新的ServletException實例中,並傳遞FacesException實例作爲根本原因,然後重新拋出ServletException實例。如果原因是ServletException的實例,請重新引發原因。如果原因是IOException的實例,請重新引發原因。否則,請創建一個新的ServletException實例,從原因傳遞消息作爲第一個參數,並將原因本身作爲第二個參數傳遞。

換句話說,它會總是罰球要麼ServletExceptionIOException。您需要使用Throwable#getCause()從捕獲的ServletException中提取所需的原因,然後進一步確定它。例如。

if (e.getCause() instanceof NullPointerException) { 
    // ... 
} 
+0

嗨!我知道我必須經歷4個e.getCause(),像這樣: e.getCause()。getCause()。getCause()。getCause()。 我可能需要循環,直到我得到一個空結果,前一個將是我的例外。 你有關於我的第二個問題的答案 - 如何從我拋出異常的地方繼續?我的意思是,我想捕獲異常並返回到bean中的用戶代碼,就好像它是bean中的常規try {} catch {}一樣。 – Rivki 2010-05-27 06:52:10