Skip to content

67 清理资源 ​

finally 在 try 退出时执行,即使 try 已准备返回值也是如此。输出顺序展示先关闭再返回调用方。

norm
Integer read() {
  try {
    printLine("open")
    return 7
  } finally {
    printLine("close")
  }
}

main() {
  printLine(read())
}

预期输出:

text
open
close
7

动手试试:把 return 7 改成 return 9,确认清理顺序不变。

精确规则:参考。

Norm 0.25