java - finally 之后带有 catch 的嵌套 try 块
问题描述
我想写这样的代码:
try {
try {
someStuffThatCausesBusinessExceptions();
} finally {
try {
cleanUp();
} catch (Exception e) {
// I don't really care
}
}
} catch (BusinessLogicException e) {
// work with exception
// cleaning up must be done by that point (or at least tried to)
}
业务逻辑的异常是否会在清理期间可能的中断中存活下来?有没有更好的方法来忽略 cleanUp 中所有可能的异常?
解决方案
catch
块只会捕获Throwables
在其相应 try
块中抛出的内容。因此,您在周围try
块中抛出的异常将被保留并在外部catch
块中捕获。
推荐阅读
- git - 有没有办法将当前项目与 git 分支进行比较并创建差异补丁
- ios - Websocket lib 是否能够在 Swift 中发送或接收 json 对象?
- html - 对齐标题?
- java - 任何优化的解决方案:获取位于同一位置的两个列表中的元素的索引
- r - 创建决策树并进行超参数调整 R?
- c# - 单元测试活动功能时,IBinder 的模拟失败
- java - 委托身份验证的可接受使用策略问题
- json - 使用 Swift 4 JSONDecoder.DataDecodingStrategy.custom(_:) 自定义 JSON 数据解码
- eclipse - eclipse 使用正则表达式搜索以小写开头后跟大写的单词
- graphql - 嵌套变异 GraphQL