I'm having an issue in my program in the part that I'm loading a serialized file. I want to fail nicely if the file can't be deserialzed, but for some reason, my program will break rather than go into the catch clause. Here's my code
using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open))
{
try
{
BinaryFormatter bf = new BinaryFormatter();
document = (Document)bf.Deserialize(fs);
}
catch (SerializationException se)
{
MessageBox.Show("Error opening this file due to serialization", se.Source);
}
catch (Exception se)
{
MessageBox.Show("Error opening this file due to serialization", se.Source);
}
}
Running this causes the program to break on the Deserialize() line. This is the exception that it throws:
Type 'Source' in Assembly 'DocumentDesigner, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
I know how to fix the exception because I commented out a couple [Serializable] attributes to test this, but I just want to know why the try clause isn't working.