This documentation is archived and is not being maintained.

Exception Constructor (String, Exception)

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.

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

Public Sub New ( _
	message As String, _
	innerException As Exception _


Type: System.String
The error message that explains the reason for the exception.
Type: System.Exception
The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.

An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the InnerException property. The InnerException property returns the same value that is passed into the constructor, or a null reference (Nothing in Visual Basic) if the InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of Exception.




The inner exception reference.


The error message string.

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.

' 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

                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 )
                Dim count As Integer = 0

                    log.AddRecord( _
                        String.Format( _
                            "Log record number {0}", count ) )
                    count += 1

            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()

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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