Export (0) Print
Expand All

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
Assemblies:   System.Runtime (in System.Runtime.dll)
  mscorlib (in mscorlib.dll)

	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 namespace System;

namespace NDP_UE_CPP

   // Derive an exception with a specifiable message and inner exception. 
   public ref class LogTableOverflowException: public Exception
      static String^ overflowMessage =  "The log table has overflowed.";

         : Exception( overflowMessage )

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

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


   public ref class LogTable
      LogTable( int numElements )
         logArea = gcnew array<String^>(numElements);
         elemInUse = 0;

      int elemInUse;


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



   // Create a log table and force an overflow. 
   void ForceOverflow()
      LogTable^ log = gcnew LogTable( 4 );
         for ( int count = 1; ; count++ )
            log->AddRecord( String::Format( "Log record number {0}", count ) );

      catch ( Exception^ ex ) 
         Console::WriteLine( ex->ToString() );



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

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

.NET Framework

Supported in: 4.6, 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

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft