C# Exception Exception(SerializationInfo, StreamingContext)

In this chapter you will learn:

  1. Get to know Exception.Exception(SerializationInfo, StreamingContext)
  2. Syntax for Exception.Exception(SerializationInfo, StreamingContext)
  3. Parameter for Exception.Exception(SerializationInfo, StreamingContext)
  4. Example - Exception.Exception(SerializationInfo, StreamingContext)

Description

Exception Exception(SerializationInfo, StreamingContext) initializes a new instance of the Exception class with serialized data.

Syntax

Exception.Exception(SerializationInfo, StreamingContext) has the following syntax.


protected Exception(
  SerializationInfo info,
  StreamingContext context
)

Parameters

Exception.Exception(SerializationInfo, StreamingContext) 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.

Example

The following code example defines a derived serializable Exception class.


//  w  ww. j  a va2s  .com


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://java2s.com";
            Source = "Exception_Class_Samples";
        }
        protected SecondLevelException( SerializationInfo info, 
            StreamingContext context ) :
                base( info, context )
        { }

        [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
        public override void GetObjectData( SerializationInfo info, 
            StreamingContext context ) 
        {
            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.", new Exception("asdf") );

                    }
                    catch( SerializationException se )
                    {
                        Console.WriteLine( "Failed to serialize: {0}", 
                            se.ToString( ) );
                    }
                    finally
                    {
                        stream.Close( );
                    }
                }
            }
            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.Exception(String, Exception)
  2. Syntax for Exception.Exception(String, Exception)
  3. Parameter for Exception.Exception(String, Exception)
  4. Example - Exception.Exception(String, Exception)
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