SerializationException Class

Namespace: System.Runtime.Serialization

Represents errors that occur during serialization or deserialization of objects.

Overview

The SerializationException class is a fundamental exception type in the .NET Framework used to signal issues that arise when attempting to serialize an object to a data stream or deserialize an object from a data stream. This can happen for various reasons, including:

  • The object being serialized is not marked with the [Serializable] attribute.
  • An issue with the serialization binder or formatter.
  • A network error or corrupted data stream during deserialization.
  • Problems with custom serialization logic.

When a serialization operation fails, the runtime throws a SerializationException, providing details about the specific cause of the failure. It's crucial to handle this exception appropriately in your applications to gracefully manage serialization-related errors.

Syntax

public sealed class SerializationException : SystemException

Remarks

The SerializationException class inherits from SystemException, which is the base class for all exceptions that occur in the .NET Framework class library. This exception provides standard properties such as Message, InnerException, and StackTrace to help diagnose the problem.

To ensure that an object can be serialized, it must be marked with the [Serializable] attribute. If this attribute is missing and you attempt to serialize the object, a SerializationException will be thrown.

Constructors

Name Description
SerializationException() Initializes a new instance of the SerializationException class.
SerializationException(string message) Initializes a new instance of the SerializationException class with a specified error message.
SerializationException(string message, Exception innerException) Initializes a new instance of the SerializationException class with a specified error message and a reference to the inner exception that is the cause of the current exception.

Properties

Name Description
Message Gets the error message and the ID of the detached serialization.
InnerException Gets a reference to the inner exception that is the cause of the current exception.
StackTrace Gets a string representation of the immediate frame in the call stack.

Example

The following C# code demonstrates how to catch a SerializationException during deserialization:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class MyData
{
    public int Value { get; set; }
    public string Name { get; set; }
}

public class SerializationExample
{
    public static void Main(string[] args)
    {
        MyData dataToWrite = new MyData { Value = 10, Name = "Sample" };
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();

        // Simulate serialization
        try
        {
            formatter.Serialize(stream, dataToWrite);
            Console.WriteLine("Serialization successful.");
        }
        catch (SerializationException ex)
        {
            Console.WriteLine($"Serialization error: {ex.Message}");
        }

        // Reset stream for deserialization
        stream.Position = 0;

        // Simulate deserialization with potential error
        try
        {
            MyData dataRead = (MyData)formatter.Deserialize(stream);
            Console.WriteLine($"Deserialization successful. Value: {dataRead.Value}, Name: {dataRead.Name}");
        }
        catch (SerializationException ex)
        {
            Console.WriteLine($"Deserialization error: {ex.Message}");
            // Log the error or perform other error handling
        }
        catch (Exception ex) // Catch other potential exceptions
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
        finally
        {
            stream.Close();
        }
    }
}

See Also

System.SystemException
System.SerializableAttribute
Serialization in .NET