Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Exception.GetObjectData Method (SerializationInfo, StreamingContext)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

When overridden in a derived class, sets the SerializationInfo with information about the exception.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public virtual void GetObjectData(
	SerializationInfo info,
	StreamingContext context


Type: System.Runtime.Serialization.SerializationInfo

The SerializationInfo that holds the serialized object data about the exception being thrown.

Type: System.Runtime.Serialization.StreamingContext

The StreamingContext that contains contextual information about the source or destination.

Exception Condition

The info parameter is a null reference (Nothing in Visual Basic).

GetObjectData sets a SerializationInfo with all the exception object data targeted for serialization. During deserialization, the exception is reconstituted from the SerializationInfo transmitted over the stream.

The following code example defines a derived serializable Exception class that implements GetObjectData, which makes minor changes to two properties and then calls the base class to perform the serialization. The example forces a divide-by-0 error and then creates an instance of the derived exception. The code serializes the instance to a file, deserializes the file into a new exception, which it throws, and then catches and displays the exception's data.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Security.Permissions;

 // Define a serializable derived exception class.
 class SecondLevelException : Exception, ISerializable
     // This public constructor is used by class instantiators.
     public SecondLevelException( string message, Exception inner ) :
         base( message, inner )
         HelpLink = "";
         Source = "Exception_Class_Samples";

     // This protected constructor is used for deserialization.
     protected SecondLevelException( SerializationInfo info, 
         StreamingContext context ) :
             base( info, context )
     { }

     // GetObjectData performs a custom serialization.
     public override void GetObjectData( SerializationInfo info, 
         StreamingContext context ) 
         // Change the case of two properties, and then use the 
         // method of the base class.
         HelpLink = HelpLink.ToLower( );
         Source = Source.ToUpperInvariant();

         base.GetObjectData( info, context );

 class SerializationDemo 
     public static void Main() 
             "This example of the Exception constructor " +
             "and Exception.GetObjectData\nwith Serialization" +
             "Info and StreamingContext parameters " +
             "generates \nthe following output.\n" );

             // This code forces a division by 0 and catches the 
             // resulting exception.
                 int  zero = 0;
                 int  ecks = 1 / zero;
             catch( Exception ex )
                 // Create a new exception to throw again.
                 SecondLevelException newExcept =
                     new SecondLevelException( 
                         "Forced a division by 0 and threw " +
                         "another exception.", ex );

                     "Forced a division by 0, caught the " +
                     "resulting exception, \n" +
                     "and created a derived exception:\n" );
                 Console.WriteLine( "HelpLink: {0}", 
                     newExcept.HelpLink );
                 Console.WriteLine( "Source:   {0}", 
                     newExcept.Source );

                 // This FileStream is used for the serialization.
                 FileStream stream = 
                     new FileStream( "NewException.dat", 
                         FileMode.Create );

                     // Serialize the derived exception.
                     SoapFormatter formatter = 
                         new SoapFormatter( null,
                             new StreamingContext( 
                                 StreamingContextStates.File ) );
                     formatter.Serialize( stream, newExcept );

                     // Rewind the stream and deserialize the 
                     // exception.
                     stream.Position = 0;
                     SecondLevelException deserExcept = 
                             formatter.Deserialize( stream );

                         "\nSerialized the exception, and then " +
                         "deserialized the resulting stream " +
                         "into a \nnew exception. " +
                         "The deserialization changed the case " +
                         "of certain properties:\n" );

                     // Throw the deserialized exception again.
                     throw deserExcept;
                 catch( SerializationException se )
                     Console.WriteLine( "Failed to serialize: {0}", 
                         se.ToString( ) );
                     stream.Close( );
         catch( Exception ex )
             Console.WriteLine( "HelpLink: {0}", ex.HelpLink );
             Console.WriteLine( "Source:   {0}", ex.Source );

             Console.WriteLine( );
             Console.WriteLine( ex.ToString( ) );
This example displays the following output.

Forced a division by 0, caught the resulting exception,
and created a derived exception:

Source:   Exception_Class_Samples

Serialized the exception, and then deserialized the resulting stream into a
new exception. The deserialization changed the case of certain properties:


NDP_UE_CS.SecondLevelException: Forced a division by 0 and threw another except
ion. ---> System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CS.SerializationDemo.Main()
   --- End of inner exception stack trace ---
   at NDP_UE_CS.SerializationDemo.Main()


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

.NET Framework
Available since 1.1
Return to top
© 2018 Microsoft