考虑这段代码:
FileOutputStream stream=null;
ObjectOutputStream objStr=null;
try
{
stream=new FileOutputStream(defaultFile);
objStr=new ObjectOutputStream(stream);
objStr.writeObject(obj);
objStr.close();
}
catch(FileNotFoundException e)
{
System.out.println("Il file "+ defaultFile+ " non è stato trovato\n");
}
catch(IOException e)
{
stream.close();
System.out.println("Si è verificato un problema di I/O nell' apertura dello stream");
}
在第二个 catch 块中,我关闭了流,但我不确定是否应该关闭它。
如果 ObjectOutputStream 的构造函数失败,它将进入第二个捕获,但我确定在这种情况下,FileOutputStream 保持打开状态吗?
我应该写一个 finally 块来处理所有异常吗?
我很难弄清楚所有情况。