finally块的问题(finally block does not complete normally) (转)
當finall塊中包含return語句時,Eclipse會給出警告“finally block does not complete normally”,原因分析如下:
1、不管try塊、catch塊中是否有return語句,finally塊都會執行。
2、finally塊中的return語句會覆蓋前面的return語句(try塊、catch塊中的return語句),所以如果finally塊中有return語句,Eclipse編譯器會報警告“finally block does not complete normally”。
3、如果finally塊中包含了return語句,即使前面的catch塊重新拋出了異常,則調用該方法的語句也不會獲得catch塊重新拋出的異常,而是會得到finally塊的返回值,并且不會捕獲異常。
?
結論,
finally 內部使用 return 語句是一種很不好的習慣,如果try中還有return語句,它會覆蓋了try 區域中 return 語句的返回值,程序的可讀性差
面對上述情況,其實更合理的做法是,既不在 try block 內部中使用 return 語句,也不在 finally 內部使用 return 語句,而應該在 finally 語句之后使用 return 來表示函數的結束和返回
http://blog.csdn.net/luckarecs/article/details/7214826
?
轉載于:https://www.cnblogs.com/softidea/p/4106490.html
總結
以上是生活随笔為你收集整理的finally块的问题(finally block does not complete normally) (转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软System Center Oper
- 下一篇: DB2更改数据文件路径