Re: [問題] 關於IO內close的問題
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):