try-catch语句中的“ finally”子句如何有用?

时间:2019-09-22 21:21:12

标签: try-catch-finally

采用以下代码:

try {
  trySomething();
} catch (Exception e) {
  //handle the exception
} finally {
  //code to run after try-catch
}
//rest of the program

这与此有何不同?

try {
  trySomething();
} catch (Exception e) {
  //handle the exception
}
//code to run after try-catch
//rest of the program

finally仅对组织有用吗?

您是否也将程序的其余代码放在finally中?

1 个答案:

答案 0 :(得分:0)

最后就是总是被称为的东西,例如关闭文件句柄或数据库连接。即使发生错误,您也想发生的事情。