try catch 的 final 块有什么用,直接写在外面不是一样?

查看 45|回复 3
作者:drymonfidelia   
return 也会执行
ysc3839   
try 或 catch 可以 return ,return 之后外面的就不跑了,但 final 会照样跑
google2020   
其他语言不清楚,js 的 final 块可以改变 try/catch 中已经 return 的返回值
mxT52CRuqR6o5   
楼上已经说过 return 的时候 finally 会执行,把必须执行的代码写在 finally 里面而不是后面对阅读更好。另外就是
try { throw something; }
catch { throw; } // 重新 throw
finally { other; }
another;
里面 other 是会执行的,another 不会执行。当然你可以说 another 可以挪到最外面的 catch 后面,但是这样会导致整个程序不能非平凡地使用方法,因为最外面的 catch ,在一段“正常书写”的代码里,不一定在当前方法里面。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部