This documentation is archived and is not being maintained.

Exception Constructor

.NET Framework 1.1

Initializes a new instance of the Exception class.

Overload List

Initializes a new instance of the Exception class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public Exception();
[C++] public: Exception();
[JScript] public function Exception();

Initializes a new instance of the Exception class with a specified error message.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public Exception(string);
[C++] public: Exception(String*);
[JScript] public function Exception(String);

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

[Visual Basic] Protected Sub New(SerializationInfo, StreamingContext)
[C#] protected Exception(SerializationInfo, StreamingContext);
[C++] protected: Exception(SerializationInfo*, StreamingContext);
[JScript] protected function Exception(SerializationInfo, StreamingContext);

Initializes a new instance of the Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Exception)
[C#] public Exception(string, Exception);
[C++] public: Exception(String*, Exception*);
[JScript] public function Exception(String, Exception);

Example

[Visual Basic, C#, C++] The following code example derives an Exception for a specific condition. The code demonstrates the use of the constructor that takes a message and an inner exception as parameters, for both the derived class and the base Exception class.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the Exception constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Sample for Exception( String, Exception ) constructor.
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Derive an exception with a specifiable message and inner exception.
    Class LogTableOverflowException
        Inherits Exception

        Private Const overflowMessage As String = _
            "The log table has overflowed."
           
        Public Sub New( )
            MyBase.New( overflowMessage )
        End Sub ' New
           
        Public Sub New( auxMessage As String )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ) )
        End Sub ' New
           
        Public Sub New( auxMessage As String, inner As Exception )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ), inner )
        End Sub ' New
    End Class ' LogTableOverflowException

    Class LogTable
       
        Public Sub New( numElements As Integer )
            logArea = New String( numElements ) { }
            elemInUse = 0
        End Sub ' New
           
        Protected logArea( ) As String
        Protected elemInUse As Integer
           
        ' The AddRecord method throws a derived exception 
        ' if the array bounds exception is caught.
        Public Function AddRecord( newRecord As String ) As Integer

            Try
                Dim curElement as Integer = elemInUse
                logArea( elemInUse ) = newRecord
                elemInUse += 1
                Return curElement

            Catch ex As Exception
                Throw New LogTableOverflowException( String.Format( _
                    "Record ""{0}"" was not logged.", newRecord ), ex )
            End Try
        End Function ' AddRecord
        End Class ' LogTable

        Module OverflowDemo
           
        ' Create a log table and force an overflow.
        Sub Main()
            Dim log As New LogTable(4)
              
            Console.WriteLine( _
                "This example of the Exception( String, Exception )" & _
                vbCrLf & "constructor generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Example of a derived exception " & vbCrLf & _
                "that references an inner exception:" & vbCrLf )
            Try
                Dim count As Integer = 0
                 
                Do
                    log.AddRecord( _
                        String.Format( _
                            "Log record number {0}", count ) )
                    count += 1
                Loop

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub ' Main

    End Module ' OverflowDemo
End Namespace ' NDP_UE_VB

' This example of the Exception( String, Exception )
' constructor generates the following output.
' 
' Example of a derived exception
' that references an inner exception:
' 
' NDP_UE_VB.LogTableOverflowException: The log table has overflowed. - Record "
' Log record number 5" was not logged. ---> System.IndexOutOfRangeException: In
' dex was outside the bounds of the array.
'    at NDP_UE_VB.LogTable.AddRecord(String newRecord)
'    --- End of inner exception stack trace ---
'    at NDP_UE_VB.LogTable.AddRecord(String newRecord)
'    at NDP_UE_VB.OverflowDemo.Main()

[C#] 
// Example for the Exception( string, Exception ) constructor.
using System;

namespace NDP_UE_CS
{
    // Derive an exception with a specifiable message and inner exception.
    class LogTableOverflowException : Exception
    {
        const string overflowMessage = 
            "The log table has overflowed.";

        public LogTableOverflowException( ) :
            base( overflowMessage )
        { }

        public LogTableOverflowException( string auxMessage ) :
            base( String.Format( "{0} - {1}", 
                overflowMessage, auxMessage ) )
        { }

        public LogTableOverflowException( 
            string auxMessage, Exception inner ) :
                base( String.Format( "{0} - {1}", 
                    overflowMessage, auxMessage ), inner )
        { }
    }

    class LogTable
    {
        public LogTable( int numElements )
        {
            logArea = new string[ numElements ];
            elemInUse = 0;
        }

        protected string[ ] logArea;
        protected int       elemInUse;

        // The AddRecord method throws a derived exception 
        // if the array bounds exception is caught.
        public    int       AddRecord( string newRecord )
        {
            try
            {
                logArea[ elemInUse ] = newRecord;
                return elemInUse++;
            }
            catch( Exception ex )
            {
                throw new LogTableOverflowException( 
                    String.Format( "Record \"{0}\" was not logged.", 
                        newRecord ), ex );
            }
        }
    }

    class OverflowDemo 
    {
        // Create a log table and force an overflow.
        public static void Main() 
        {
            LogTable log = new LogTable( 4 );

            Console.WriteLine( 
                "This example of the Exception( string, Exception )" +
                "\nconstructor generates the following output." );
            Console.WriteLine( 
                "\nExample of a derived exception " +
                "that references an inner exception:\n" );
            try
            {
                for( int count = 1; ; count++ )
                {
                    log.AddRecord( 
                        String.Format( 
                            "Log record number {0}", count ) );
                }
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }
    }
}

/*
This example of the Exception( string, Exception )
constructor generates the following output.

Example of a derived exception that references an inner exception:

NDP_UE_CS.LogTableOverflowException: The log table has overflowed. - Record "Lo
g record number 5" was not logged. ---> System.IndexOutOfRangeException: Index
was outside the bounds of the array.
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   --- End of inner exception stack trace ---
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()
*/

[C++] 
// Example for the Exception( String*, Exception* ) constructor.
#using <mscorlib.dll>
using namespace System;

namespace NDP_UE_CPP
{
    // Derive an exception with a specifiable message and inner exception.
    public __gc class LogTableOverflowException : public Exception
    {
        static String* overflowMessage = 
            "The log table has overflowed.";

    public: 
        LogTableOverflowException( ) :
            Exception( overflowMessage )
        { }

        LogTableOverflowException( String* auxMessage ) :
            Exception( String::Format( S"{0} - {1}", 
                overflowMessage, auxMessage ) )
        { }

        LogTableOverflowException( 
            String* auxMessage, Exception* inner ) :
                Exception( String::Format( S"{0} - {1}", 
                    overflowMessage, auxMessage ), inner )
        { }
    };

    public __gc class LogTable
    {
    public: 
        LogTable( int numElements )
        {
            logArea = new String*[ numElements ];
            elemInUse = 0;
        }

    protected: 
        String*   logArea[ ];
        int       elemInUse;

    // The AddRecord method throws a derived exception 
    // if the array bounds exception is caught.
    public:
        int       AddRecord( String* newRecord )
        {
            try
            {
                logArea[ elemInUse ] = newRecord;
                return elemInUse++;
            }
            catch( Exception* ex )
            {
                throw new LogTableOverflowException( 
                    String::Format( S"Record \"{0}\" was not logged.", 
                        newRecord ), ex );
            }
        }
    };

    // Create a log table and force an overflow.
    void ForceOverflow( )
    {
        LogTable* log = new LogTable( 4 );

        try
        {
            for( int count = 1; ; count++ )
            {
                log->AddRecord( 
                    String::Format( S"Log record number {0}", 
                        __box(count) ) );
            }
        }
        catch( Exception* ex )
        {
            Console::WriteLine( ex->ToString( ) );
        }
    }
}

void main() 
{
    Console::WriteLine( 
        S"This example of the Exception( String*, Exception* )\n" 
        S"constructor generates the following output." );
    Console::WriteLine( 
        S"\nExample of a derived exception " 
        S"that references an inner exception:\n" );

    NDP_UE_CPP::ForceOverflow( );
}

/*
This example of the Exception( String*, Exception* )
constructor generates the following output.

Example of a derived exception that references an inner exception:

NDP_UE_CPP.LogTableOverflowException: The log table has overflowed. - Record "L
og record number 5" was not logged. ---> System.IndexOutOfRangeException: Index
 was outside the bounds of the array.
   at NDP_UE_CPP.LogTable.AddRecord(String newRecord)
   --- End of inner exception stack trace ---
   at NDP_UE_CPP.LogTable.AddRecord(String newRecord)
   at NDP_UE_CPP.ForceOverflow()
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Exception Class | Exception Members | System Namespace

Show: