This documentation is archived and is not being maintained.

Exception::Message Property

Gets a message that describes the current exception.

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

virtual property String^ Message {
	String^ get ();

Property Value

Type: System::String
The error message that explains the reason for the exception, or an empty string("").



The text of Message should completely describe the error and should, when possible, explain how to correct it. The value of the Message property is included in the information returned by ToString.

The Message property is set only when creating an Exception. If no message was supplied to the constructor for the current instance, the system supplies a default message that is formatted using the current system culture.

Notes to Implementers

If you throw an exception from a property, and you need to refer in the text of Message to the property argument that you set or get, use "value" as the name of the property argument.

Notes to Inheritors

The Message property is overridden in classes that require control over message content or format. Application code typically accesses this property when it needs to display information about an exception that has been caught.

The error message should be localized.

The following code example throws and then catches an Exception and displays the exception's text message using the Message property.

// Example for the Exception::HelpLink, Exception::Source,
// Exception::StackTrace, and Exception::TargetSite properties.
using namespace System;

namespace NDP_UE_CPP

   // Derive an exception; the constructor sets the HelpLink and 
   // Source properties.
   public ref class LogTableOverflowException: public Exception
      static String^ overflowMessage = "The log table has overflowed.";

      LogTableOverflowException( String^ auxMessage, Exception^ inner )
         : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ), inner )
         this->HelpLink = "";
         this->Source = "Exception_Class_Samples";


   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( "\nMessage ---\n{0}", ex->Message );
         Console::WriteLine( "\nHelpLink ---\n{0}", ex->HelpLink );
         Console::WriteLine( "\nSource ---\n{0}", ex->Source );
         Console::WriteLine( "\nStackTrace ---\n{0}", ex->StackTrace );
         Console::WriteLine( "\nTargetSite ---\n{0}", ex->TargetSite->ToString() );



int main()
   Console::WriteLine( "This example of \n   Exception::Message, \n"
   "   Exception::HelpLink, \n   Exception::Source, \n"
   "   Exception::StackTrace, and \n   Exception::"
   "TargetSite \ngenerates the following output." );

This example of
   Exception::StackTrace, and
generates the following output.

Message ---
The log table has overflowed. - Record "Log record number 5" was not logged.

HelpLink ---

Source ---

StackTrace ---
   at NDP_UE_CPP.LogTable.AddRecord(String newRecord)
   at NDP_UE_CPP.ForceOverflow()

TargetSite ---
Int32 AddRecord(System.String)

.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.