I'll give an example in c#.
The following two listings achieve the same thing. Case 2 is definitely better styled than case 1 as the try section isolates the line that throws the exception.
I am very curious to know if the performance is different and, if yes, how does it scale up with the quantity of code included in the try section?
And, finally, why is that?
Case 1:
try
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
data = (PlayerData)bf.Deserialize(file);
file.Close();
} catch (System.Runtime.Serialization.SerializationException e)
{
...
}
Case 2:
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
try
{
data = (PlayerData)bf.Deserialize(file);
} catch (System.Runtime.Serialization.SerializationException e)
{
}
file.Close();