【问题标题】:How to preserve stack trace in this case?在这种情况下如何保留堆栈跟踪?
【发布时间】:2013-09-11 05:37:37
【问题描述】:

在我的代码中,我在声纳上分析代码时遇到了一个名为“保留堆栈跟踪”的规则违规。

try {
  doSomething();
} catch(IllegalStateException e) {
   try {
      doAnotherThing();
  } catch(IOException e1) {
      throw new MyException(e1.getCause());
  }
  throw new MyException(e.getCause());
}

那么在这种情况下如何保存堆栈跟踪呢?

【问题讨论】:

    标签: java exception sonarqube stack-trace pmd


    【解决方案1】:

    你应该分别抛出 MyException(e) 和 MyException(e1)。

    【讨论】:

    • 我已将我的代码更改为 try { doSomething(); } catch(IllegalStateException e) { try { doAnotherThing(); } catch(IOException e1) { throw new MyException(e1); } throw new MyException(e); } 仍然给我代码违规...
    【解决方案2】:

    改变

    throw new MyException(exception.getCause());
    

    throw new MyException(exception);
    

    这将保留完整的堆栈跟踪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2010-10-18
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多