.NET Framework Class Library
ExceptionMessage Property

Gets a message that describes the current exception.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Overridable ReadOnly Property Message As [%$TOPIC/9btwf6wk_en-us_VS_110_1_0_0_0_0%]
public virtual [%$TOPIC/9btwf6wk_en-us_VS_110_1_0_1_0_0%] Message { get; }
public:
virtual property [%$TOPIC/9btwf6wk_en-us_VS_110_1_0_2_0_0%]^ Message {
	[%$TOPIC/9btwf6wk_en-us_VS_110_1_0_2_0_1%]^ get ();
}
abstract Message : [%$TOPIC/9btwf6wk_en-us_VS_110_1_0_3_0_0%] with get 
override Message : [%$TOPIC/9btwf6wk_en-us_VS_110_1_0_3_0_1%] with get

Property Value

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

Implements

_ExceptionMessage
Remarks

Error messages target the developer who is handling the exception. The text of the Message property should completely describe the error and, when possible, should also explain how to correct the error. Top-level exception handlers may display the message to end-users, so you should ensure that it is grammatically correct and that each sentence of the message ends with a period. Do not use question marks or exclamation points. If your application uses localized exception messages, you should ensure that they are accurately translated.

Security noteSecurity Note

Do not disclose sensitive information in exception messages without checking for the appropriate permissions.

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.

Windows Runtime and .NET Framework 4.5.1

Starting with the .NET Framework 4.5.1 and Windows 8.1, the fidelity of error messages from exceptions that are propagated from Windows Runtime types and members that are not part of the .NET Framework is improved. In particular, exception messages from Visual C++ component extensions (C++/CX) are now propagated back into .NET Framework Exception objects.

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.

Examples

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

' Example for the Exception.HelpLink, Exception.Source, 
' Exception.StackTrace, and Exception.TargetSite properties. 
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Derive an exception; the constructor sets the HelpLink and  
    ' Source properties. 
    Class LogTableOverflowException
        Inherits Exception

        Private Const overflowMessage As String = _
            "The log table has overflowed." 

        Public Sub New( auxMessage As String, inner As Exception )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ), inner )

            Me.HelpLink = "http://msdn.microsoft.com" 
            Me.Source = "Exception_Class_Samples" 

        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 

            Try 
                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 " & vbCrLf & _
                "   Exception.Message, " & vbCrLf & _
                "   Exception.HelpLink, " & vbCrLf & _
                "   Exception.Source, " & vbCrLf & _
                "   Exception.StackTrace, and " & vbCrLf & _
                "   Exception.TargetSite " & vbCrLf & _
                "generates the following output." )

            Try 
                Dim count As Integer = 0

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

            Catch ex As Exception
                Console.WriteLine( vbCrLf & _
                    "Message ---" & vbCrLf & ex.Message )
                Console.WriteLine( vbCrLf & _
                    "HelpLink ---" & vbCrLf & ex.HelpLink )
                Console.WriteLine( vbCrLf & _
                    "Source ---" & vbCrLf & ex.Source )
                Console.WriteLine( vbCrLf & _
                    "StackTrace ---" & vbCrLf & ex.StackTrace )
                Console.WriteLine( vbCrLf & "TargetSite ---" & _
                    vbCrLf & ex.TargetSite.ToString( ) )
            End Try 
        End Sub ' Main

    End Module ' OverflowDemo
End Namespace ' NDP_UE_VB

' This example of 
'    Exception.Message, 
'    Exception.HelpLink, 
'    Exception.Source, 
'    Exception.StackTrace, and 
'    Exception.TargetSite 
' generates the following output. 
'  
' Message --- 
' The log table has overflowed. - Record "Log record number 5" was not logged. 
'  
' HelpLink --- 
' http://msdn.microsoft.com 
'  
' Source --- 
' Exception_Class_Samples 
'  
' StackTrace --- 
'    at NDP_UE_VB.LogTable.AddRecord(String newRecord) 
'    at NDP_UE_VB.OverflowDemo.Main() 
'  
' TargetSite --- 
' Int32 AddRecord(System.String)
// Example for the Exception.HelpLink, Exception.Source, 
// Exception.StackTrace, and Exception.TargetSite properties. 
using System;

namespace NDP_UE_CS
{
    // Derive an exception; the constructor sets the HelpLink and  
    // Source properties. 
    class LogTableOverflowException : Exception
    {
        const string overflowMessage = "The log table has overflowed.";

        public LogTableOverflowException( 
            string auxMessage, Exception inner ) :
                base( String.Format( "{0} - {1}", 
                    overflowMessage, auxMessage ), inner )
        {
            this.HelpLink = "http://msdn.microsoft.com";
            this.Source = "Exception_Class_Samples";
        }
    }

    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 e )
            {
                throw new LogTableOverflowException( 
                    String.Format( "Record \"{0}\" was not logged.", 
                        newRecord ), e );
            }
        }
    }

    class OverflowDemo 
    {
        // Create a log table and force an overflow. 
        public static void Main() 
        {
            LogTable log = new LogTable( 4 );

            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." );

            try
            {
                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 );
            }
        }
    }
}

/*
This example of
   Exception.Message,
   Exception.HelpLink,
   Exception.Source,
   Exception.StackTrace, and
   Exception.TargetSite
generates the following output.

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

HelpLink ---
http://msdn.microsoft.com

Source ---
Exception_Class_Samples

StackTrace ---
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()

TargetSite ---
Int32 AddRecord(System.String)
*/
// 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
   {
   private:
      static String^ overflowMessage = "The log table has overflowed.";

   public:
      LogTableOverflowException( String^ auxMessage, Exception^ inner )
         : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ), inner )
      {
         this->HelpLink = "http://msdn.microsoft.com";
         this->Source = "Exception_Class_Samples";
      }

   };

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


   protected:
      array<String^>^logArea;
      int elemInUse;

   public:

      // The AddRecord method throws a derived exception if  
      // the array bounds exception is caught. 
      int AddRecord( String^ newRecord )
      {
         try
         {
            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 );
      try
      {
         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." );
   NDP_UE_CPP::ForceOverflow();
}

/*
This example of
   Exception::Message,
   Exception::HelpLink,
   Exception::Source,
   Exception::StackTrace, and
   Exception::TargetSite
generates the following output.

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

HelpLink ---
http://msdn.microsoft.com

Source ---
Exception_Class_Samples

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

TargetSite ---
Int32 AddRecord(System.String)
*/
Version Information

.NET Framework

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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

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.