Skip to content

Latest commit

 

History

History
56 lines (38 loc) · 2.18 KB

binaryformatter-deserialize-rewraps-exceptions.md

File metadata and controls

56 lines (38 loc) · 2.18 KB
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

BinaryFormatter.Deserialize rewraps some exceptions in SerializationException

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.

Change description

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.

Version introduced

5.0

Recommended action

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.
}

Affected APIs

  • xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=fullName