Export (0) Print
Expand All

Exception Constructor (SerializationInfo, StreamingContext)

Initializes a new instance of the Exception class with serialized data.

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

	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.


The info parameter is nullptr.


The class name is nullptr or HResult is zero (0).

This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see [<topic://cpconSerialization>].

The following code example defines a derived serializable Exception class. The code forces a divide-by-0 error and then creates an instance of the derived exception using the (SerializationInfo, StreamingContext) constructor. 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.

// Example for the Exception( SerializationInfo, StreamingContext ) 
// constructor and the Exception.GetObjectData( SerializationInfo,  
// StreamingContext ) method.
#using <System.Runtime.Serialization.Formatters.Soap.dll>

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Soap;

// Define a serializable derived exception class.

ref class SecondLevelException: public Exception, public ISerializable

   // This public constructor is used by class instantiators.
   SecondLevelException( String^ message, Exception^ inner )
      : Exception( message, inner )
      HelpLink = "http://MSDN.Microsoft.com";
      Source = "Exception_Class_Samples";


   // This protected constructor is used for deserialization.
   SecondLevelException( SerializationInfo^ info, StreamingContext context )
      : Exception( info, context )


   // GetObjectData performs a custom serialization.
   virtual void GetObjectData( SerializationInfo^ info, StreamingContext context ) override

      // Change the case of two properties, and then use the  
      // method of the base class.
      HelpLink = HelpLink->ToLower();
      Source = Source->ToUpper();
      Exception::GetObjectData( info, context );


int main()
   Console::WriteLine( "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 = gcnew SecondLevelException( "Forced a division by 0 and threw " 
         "another exception.",ex );
         Console::WriteLine( "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 = gcnew FileStream( "NewException.dat",FileMode::Create );

            // Serialize the derived exception.
            SoapFormatter^ formatter = gcnew SoapFormatter( nullptr,StreamingContext(StreamingContextStates::File) );
            formatter->Serialize( stream, newExcept );

            // Rewind the stream and deserialize the  
            // exception.
            stream->Position = 0;
            SecondLevelException^ deserExcept = dynamic_cast<SecondLevelException^>(formatter->Deserialize( stream ));
            Console::WriteLine( "\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() );


   catch ( Exception^ ex ) 
      Console::WriteLine( "HelpLink: {0}", ex->HelpLink );
      Console::WriteLine( "Source:   {0}", ex->Source );
      Console::WriteLine( ex->ToString() );


This example of the Exception constructor and Exception.GetObjectData
with SerializationInfo and StreamingContext parameters generates
the following output.

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

HelpLink: http://MSDN.Microsoft.com
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:

HelpLink: http://msdn.microsoft.com

SecondLevelException: Forced a division by 0 and threw another exception. ---> S
ystem.DivideByZeroException: Attempted to divide by zero.
   at main()
   --- End of inner exception stack trace ---
   at main()


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

© 2014 Microsoft