我编写了自己的异常类,并且在我的操作类中抛出了这个异常,我有一个过滤器,我的请求在从过滤器传递后由操作处理,如果我的操作类抛出我自己的异常,它不会被过滤器捕获。请让我知道为什么会这样。
这是我的过滤器
public class myfilter implements Filter
{
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)throws IOException, ServletException
{
try
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
RequestDispatcher rd = req.getRequestDispatcher("/"+url);
if(rd != null)
{
rd.forward(request,response);
}
}
catch(MyException ex)
{
System.out.println("filter caught exp--"+ex.getMessage());
ex.printStackTrace();
throw new ServletException(ex.getMessage());
}
}
public void init(FilterConfig filterConfig)
{
}
public void destroy()
{
System.out.println("--filter destroy--");
}
}
我的结构动作类在这里
public class eventaction extends Action
{
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws MyException
{
response.setContentType("text/html; charset=UTF-8");
/*
Statements
*/
throw new MyException("exception");
}
}
以下行记录了一些痕迹
[14:50:22:480]|[07-04-2013]|[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/].[action]]|[SEVERE]|[12]|: Servlet.service() for servlet action threw exception|MyException: Unable_to_add_event
如果我在我的过滤器而不是 MyException 中捕获了一般异常,它会被过滤器捕获。为什么?
catch(Exception ex) //catch(MyException ex)
{
System.out.println("filter caught exp--"+ex.getMessage());
ex.printStackTrace();
throw new ServletException(ex.getMessage());
}