I can't find an answer to this. Hope anyone can help.
I have a property setter that throws an exception if the value doesn't meet certain conditions. The class where the property lives is serialized/deserialized.
When deserializing the class in .NET 2.0 the exception is caught. If I change the project target to .NET 4.0 the exception is not caught!
Any Idea?
Thanks, Sam
Sorry. By 'exception' I'm referring to one that I throw. Here's a sample code.
This is a sample class that I want serialized/deserialized:
public class MyClass
{
public string Name
{
get{return myName;}
set
{
if (value == "") throw new Exception("Name is blank!");
myName = value;
}
}
private string myName;
}
And here's the code that calls the deserializer.
try
{
XmlSerializer xs = new XmlSerializer(typeof(MyClass));
using (FileStream stream = File.OpenRead("myclass.xml"))
{
MyClass mc = xs.Deserialize(stream) as MyClass;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Here's the "myclass.xml" file to test the exception.
<?xml version="1.0"?>
<MyClass>
<Name></Name>
</MyClass>
Using .NET 2.0 I get the message box with the exception message. Using .NET 4.0 I get "Exception unhandled by user code".
Thanks,
Sam