While working on a school project, I wrote the following code:
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = new FileOutputStream(file);
How about this guys? No null check, no surprise. Everything is cleaned upon exit.
try {
final FileOutputStream fos = new FileOutputStream(file);
try {
final ObjectOutputStream oos = new ObjectOutputStream(fos);
try {
oos.writeObject(shapes);
oos.flush();
}
catch(IOException ioe) {
// notify user of important exception
}
finally {
oos.close();
}
}
finally {
fos.close();
}
}
catch (FileNotFoundException ex) {
// complain to user
}
catch (IOException ex) {
// notify user
}