【问题标题】:What happens if one filter throws Exceptions in servlet?如果一个过滤器在 servlet 中抛出异常会怎样?
【发布时间】:2018-02-21 00:03:58
【问题描述】:

我想在现有项目中添加过滤器,不要指望我的过滤器在异常情况下影响原始流程。在任何情况下,都应该执行原始过滤器。

所以,我想知道,当我发现异常时,我应该如何处理:

  1. 抛出异常
  2. 捕获异常并调用chain.doFilter();
  3. 什么都不做,像下面的代码:

    if (filter != null) {
    
        filter.doFilter(req,resp,chain);
        // should I catch the exception here?
    } else {
    
        chain.doFilter(req,resp);
    }
    

谢谢大家。

【问题讨论】:

    标签: java servlets servlet-filters


    【解决方案1】:

    try-catch 块嵌套在if(filter!=null) 中 例如:

    if(filter != null){
         try{} catch (Exception e){
            //do something
        }
    }
    

    您还可以在catch 之后使用finally 进行任何清理或您想要运行的代码,无论是否捕获到异常。

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 1970-01-01
      • 2013-09-07
      • 2018-12-27
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2015-03-28
      相关资源
      最近更新 更多