Consider this code:
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");
}
In the second catch block, I close the stream but I am not sure if it should be closed.
It enters in the second catch if the constructor of ObjectOutputStream fails, but am I sure that in this case, the FileOutputStream remains opened?
Shall I write a finally block to handle all exceptions?
It's hard for me to figure out all cases.