我在java bean中抛出NullPointerException并在FacesServletWrapper中捕获异常。 在FacesServletWrapper中,我总是只得到ServletException。
如何捕获我抛出的特定异常?
如何从抛出异常的地方继续?
在我的豆子里:
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 :(得分:0)
您在此处致电FacesServlet#service()
:
try {
super.service(request, response);
} catch (Throwable e) {
// ...
}
以下是its javadoc的摘录,以了解它可能引发的异常类型:
如果在任何一种情况下抛出
FacesException
,请从FacesException
中提取原因。如果原因是null
从FacesException
中提取消息,则将其放在新的ServletException
实例中,并将FacesException
实例作为根本原因传递,然后重新抛出ServletException
个实例。如果原因是ServletException
的实例,请重新抛出原因。如果原因是IOException
的实例,请重新抛出原因。否则,创建一个新的ServletException
实例,将原因中的消息作为第一个参数传递,将原因本身作为第二个参数传递。
换句话说,它会始终抛出ServletException
或IOException
。您需要使用Throwable#getCause()
从捕获的ServletException
中提取所需的原因,然后再进一步确定。 E.g。
if (e.getCause() instanceof NullPointerException) {
// ...
}