简体   繁体   English

如何捕获已经捕获的异常?

[英]How to catch already caught exception?

I have the follow the following filter: 我有以下过滤器:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
     ServletException {

  try {
     chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);
  } catch (XssAttackException e) {
     request.getRequestDispatcher("/XssAttack").forward(request, response);
  }
}

and the class XssAttackException is: XssAttackException类是:

public class XssAttackException extends RuntimeException {

private static final long serialVersionUID = 1L;

}

after debugging the code, I realized that somewhere in the spring framework all the exceptions are being caught. 调试代码后,我意识到在spring框架中的某个地方,所有异常都被捕获了。 Now I need a way that my catch bock also run. 现在,我需要一种方法来使捕获块也运行。

UPDATE 更新

inside XSSRequestWrapper we have: XSSRequestWrapper我们有:

@Override
public String getHeader(String name) {

  String value = super.getHeader(name);
  return stripXSS(value);
}

And

private String stripXSS(String value) {

  if (value != null) {
     value = persianUtf8(value);
     if (!value.equals(Jsoup.parse(value).text())) {
        throw new XssAttackException();
     }
     value = Jsoup.parse(value).text();
     for (Pattern scriptPattern : patterns) {
        if (scriptPattern.matcher(value).matches()) {
           throw new XssAttackException();
        }
        value = scriptPattern.matcher(value).replaceAll("");
     }
  }
  return value;
}

Please don't assume this is answer for your question.Assumed too long comment. 请不要以为这是您的问题的答案。假设评论太长。 I created my CustomException class. 我创建了CustomException类。

public class CustomException extends RuntimeException {
}

and created custom Servlet class as your XSSRequestWrapper and throw my custom exception in constructor. 并创建了自定义Servlet类作为您的XSSRequestWrapper并将我的自定义异常抛出到构造函数中。

public class MyServlet implements ServletRequest {

    public MyServlet() {
        throw new CustomException();
    }
// other override methods go here
}

and in my filter class 在我的过滤器类中

    try {
        chain.doFilter(new MyServlet(), response);
    } catch (CustomException e) {
        System.out.println("xxxxxxxxxxxxxxx I got it xxxxxxxxxxxxxxxxxxx");
    }

This code work fine. 这段代码工作正常。 At your program , I think there has some exception has occured and you did not catch on them. 在您的程序中,我认为发生了一些异常,您没有抓住它们。 So , this exception object has miss from your try block of your filter class and handled by Spring container. 因此,此异常对象在您的过滤器类的try块中未命中,并由Spring容器处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM