title | description | ms.date |
---|---|---|
Breaking change: BinaryFormatter.Deserialize rewraps some exceptions |
Learn about the breaking change in .NET 5 where BinaryFormatter.Deserialize rewraps some exception objects inside a SerializationException. |
08/18/2020 |
The xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=nameWithType method now rewraps some exception objects inside a xref:System.Runtime.Serialization.SerializationException before propagating the exception back to the caller.
Previously, the xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=nameWithType method allowed some arbitrary exceptions, such as xref:System.ArgumentNullException, to propagate up the stack to its callers.
In .NET 5 and later, the xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=nameWithType method more aggressively catches exceptions that occur due to invalid deserialization operations and wraps them in a xref:System.Runtime.Serialization.SerializationException.
5.0
In most cases, you don't need to take any action. However, if your call site depends on a particular exception being thrown, you can unwrap the exception from the outer xref:System.Runtime.Serialization.SerializationException, as shown in the following example.
Stream inputStream = GetInputStream();
var formatter = new BinaryFormatter();
try
{
object deserialized = formatter.Deserialize(inputStream);
}
catch (MyException myEx)
{
// Handle 'myEx' here in case it was thrown directly.
}
catch (SerializationException serEx) when (serEx.InnerException is MyException myEx)
{
// Handle 'myEx' here in case it was wrapped in SerializationException.
}
- xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=fullName