【问题标题】:If an exception is thrown in the catch clause, does finally still run如果在 catch 子句中抛出异常,最终仍然运行
【发布时间】:2012-04-25 01:25:32
【问题描述】:

考虑下面的代码,如果catch子句抛出异常,finally是否仍然运行?

try {
//code here throws exception
}
catch(Exception ex) {
//code catches above exception however code here also throws another exception
}
finally {
//does this code even run considering the exception thrown in the above catch clause??
}

【问题讨论】:

  • 在我看来,您可以用一两行代码替换问题中的 cmets,然后自己进行测试。你会更快地得到答案,并且在这个过程中可能会学到一两件额外的东西。 :)
  • 是的,this answer 重复并出色地回答了“finally 和 no finally 之间的区别是什么?”

标签: java exception try-catch


【解决方案1】:

是的。无论 try/catch 中发生什么,它都会运行(假设 JVM 由于某种原因没有关闭)

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多