C# Exception GetObjectData
In this chapter you will learn:
- Get to know Exception.GetObjectData
- Syntax for Exception.GetObjectData
- Parameter for Exception.GetObjectData
- Returns for Exception.GetObjectData
- 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:
- Get to know Exception.GetType
- Syntax for Exception.GetType
- Returns for Exception.GetType
- Example - Exception.GetType
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 MemberwiseClone
C# Exception ToString
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 GetTypeC# Exception MemberwiseClone
C# Exception ToString