-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
BinaryFormatter.Deserialize rewraps some exceptions in SerializationException
The BinaryFormatter.Deserialize
method will rewrap some exception objects inside a SerializationException
before propagating the exception back to the caller.
Version introduced
.NET 5.0 Preview 1
Old behavior
The Deserialize
method may allow arbitrary exceptions (such as ArgumentNullException
) to propagate up the stack to its callers.
New behavior
The Deserialize
method is more aggressive about catching exceptions that occur due to invalid deserialization operations and wrapping them within a SerializationException
.
Reason for change
This change brings the method's actual implementation more in line with its documented behavior.
Recommended action
Most applications needn't take any action.
If your call site depends on a particular exception being thrown, you may need to unwrap the exception from the outer SerializationException
. An example of this is shown below.
Stream inputStream = GetInputStream();
BinaryFormatter formatter = new BinaryFormatter();
try
{
object deserialized = formatter.Deserialize(inputStream);
}
catch (MyException myEx)
{
// handle 'myEx' here in case it was thrown directly
}
catch (SerializationException serEx)
{
if (serEx.InnerException is MyException myEx)
{
// handle 'myEx' here in case it was wrapped in SerializationException
}
else
{
throw; // this exception wasn't meant for us; rethrow it
}
}
Category
- Serialization
Affected APIs
Issue metadata
- Issue type: breaking-change