Export (0) Print
Expand All

Exception.Exception(String, Exception) Constructor

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
)
public Exception (
	String message, 
	Exception innerException
)
public function Exception (
	message : String, 
	innerException : Exception
)
Not applicable.

Parameters

message

The error message that explains the reason for the exception.

innerException

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.

Property

Value

InnerException

The inner exception reference.

Message

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 )
        {
            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()
*/

// Example for the Exception( string, Exception ) constructor.
package NDP_UE_JSL; 

import System.* ;

// Derive an exception with a specifiable message and inner exception.
class LogTableOverflowException extends System.Exception
{
    private String overflowMessage = "The log table has overflowed.";

    public LogTableOverflowException()
    {
        super("The log table has overflowed.");
    } //LogTableOverflowException

    public LogTableOverflowException(String auxMessage)
    {
        super(String.Format("The log table has overflowed. - {0}", 
            auxMessage));
    } //LogTableOverflowException

    public LogTableOverflowException(String auxMessage, Exception inner)
    {
        super(String.Format("The log table has overflowed.- {0}", auxMessage), 
            inner);
    } //LogTableOverflowException
} //LogTableOverflowException

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

    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) throws LogTableOverflowException
    {
        try {
            logArea.set_Item(elemInUse, newRecord);
            return elemInUse++;
        }
        catch (Exception ex) {
            throw new LogTableOverflowException(String.Format(
                    "Record \"{0}\" was not logged.", newRecord), ex);
        }
    } //AddRecord
} //LogTable

class OverflowDemo
{
    // Create a log table and force an overflow.
    public static void main(String[] args)
    {
        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 iCtr = 1; ; iCtr++) {
                log.AddRecord(String.Format("Log record number {0}", 
                    System.Convert.ToString(iCtr)));
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.toString());
        }
    } //main
} //OverflowDemo
 
/*
This example of the Exception( string, Exception )
constructor generates the following output.

Example of a derived exception that references an inner exception:

NDP_UE_JSL.LogTableOverflowException: The log table has overflowed.- 
Record "Log record number 5" was not logged. ---> 
java.lang.ArrayIndexOutOfBoundsException:
 Index was outside the bounds of the array.
   --- End of inner exception stack trace ---
   at NDP_UE_JSL.LogTable.AddRecord(String newRecord) in C:\
Documents and Settings\My Documents\Visual Studio Projects\ConsoleApp - JS\
ConsoleApp - JS\Class1.jsl:line 52
   at NDP_UE_JSL.OverflowDemo.main(String[] args) in C:\Documents and Settings\
My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 71
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft