Re: [問題] 關於IO內close的問題

看板java作者 (呆呆向前衝)時間18年前 (2008/01/12 12:18), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《ericsys3152 (住在巴別塔的男孩)》之銘言: : 當file丟出exception後file會不會自動執行close()啊? : 本來想寫以下的code: : RandomAccessFile test = null; : try : { : test = new RandomAccessFile("test.dat", "r"); : ... : } : catch (FileNotFoundException e) : { : ... : } : (一堆catch...) : finally : { : test.close(); : } : 可是compile時compiler說finally裡面的test要放在try裡面, : 這樣不就一直try try try try ... 無窮回圈 = = : 我是擔心丟出Exception之後file不會自動執行close()才這樣寫的啦, : 不然test.close()寫在try最後面就好了, : 還是我多慮了~"~ 當file丟出exception後file會不會自動執行close()啊? Ans:不會 如果是我的話 一般在finally的地方我會再包個try catch 但是catch就不處理任何事了 try{ }catch(){ }finally{ try{ if(test!=null) test.close(); //先判斷test不是null才close }catch(Exception e){} } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.120.70.58 ※ 編輯: daidaibruce 來自: 122.120.70.58 (01/12 12:20)

01/13 14:15, , 1F
先看信才看這篇,謝謝你的回答喔^^
01/13 14:15, 1F
文章代碼(AID): #17Y3-atj (java)
文章代碼(AID): #17Y3-atj (java)