Here is a Java example I found online:
try{
//use buffering
InputStream file = new FileInputStream("quarks.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
try{
//deserialize the List
List<String> recoveredQuarks = (List<String>)input.readObject();
//display its data
for(String quark: recoveredQuarks){
System.out.println("Recovered Quark: " + quark);
}
}
finally{
input.close();
}
} catch(ClassNotFoundException ex){
//some exception handling
}
In the above, what are the benefits of using a try-finally block to execute some processing with the input before closing the input? In other words, what benefits would the code above have over something like this:
try{
//use buffering
InputStream file = new FileInputStream("quarks.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
List<String> recoveredQuarks = (List<String>)input.readObject();
for(String quark: recoveredQuarks){
System.out.println("Recovered Quark: " + quark);
}
input.close();
} catch(ClassNotFoundException ex){
//some exception handling
}