Exception Constructor (String, Exception)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 Exception(
	string message,
	Exception innerException


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.

// 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 )
                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 );

                "This example of the Exception( string, Exception )" +
                "\nconstructor generates the following output." );
                "\nExample of a derived exception " +
                "that references an inner exception:\n" );
                for( int count = 1; ; count++ )
                            "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()

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top