C# Exception GetObjectData

In this chapter you will learn:

  1. Get to know Exception.GetObjectData
  2. Syntax for Exception.GetObjectData
  3. Parameter for Exception.GetObjectData
  4. Returns for Exception.GetObjectData
  5. Example - Exception.GetObjectData

Description

Exception GetObjectData when overridden in a derived class, sets the SerializationInfo with information about the exception.

Syntax

Exception.GetObjectData has the following syntax.


public virtual void GetObjectData(
  SerializationInfo info,
  StreamingContext context
)

Parameters

Exception.GetObjectData has the following parameters.

  • info - The SerializationInfo that holds the serialized object data about the exception being thrown.
  • context - The StreamingContext that contains contextual information about the source or destination.

Returns

Exception.GetObjectData method returns

Example

The following code example defines a derived serializable Exception class that implements GetObjectData.


/*  w w w  . ja v a  2s .co  m*/
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Security.Permissions;

[Serializable()]
class SecondLevelException : Exception, ISerializable
{
    public SecondLevelException( string message, Exception inner ) :
        base( message, inner )
    {
        HelpLink = "http://MSDN.Microsoft.com";
        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.
    [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
    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.ToUpper( );

        base.GetObjectData( info, context );
    }
}

class SerializationDemo 
{
    public static void Main() 
    {

        try
        {
                SecondLevelException newExcept =
                    new SecondLevelException( 
                        "Forced a division by 0 and threw " +
                        "another exception.", ex );
                    throw newExcept;
        }
        catch( Exception ex )
        {
            Console.WriteLine( "HelpLink: {0}", ex.HelpLink );
            Console.WriteLine( "Source:   {0}", ex.Source );

            Console.WriteLine( );
            Console.WriteLine( ex.ToString( ) );
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know Exception.GetType
  2. Syntax for Exception.GetType
  3. Returns for Exception.GetType
  4. Example - Exception.GetType
Home »
  C# Tutorial »
    System »
      Exception
C# Exception Data
C# Exception HelpLink
C# Exception HResult
C# Exception InnerException
C# Exception Message
C# Exception Source
C# Exception StackTrace
C# Exception TargetSite
C# Exception Exception()
C# Exception Exception(String)
C# Exception Exception(SerializationInfo, S...
C# Exception Exception(String, Exception)
C# Exception Equals(Object)
C# Exception Finalize
C# Exception GetBaseException
C# Exception GetHashCode
C# Exception GetObjectData
C# Exception GetType
C# Exception MemberwiseClone
C# Exception ToString