Was this page helpful?
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Exception.Message Property


Gets a message that describes the current exception.

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

public virtual string Message { get; }

Property Value

Type: System.String

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

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.

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

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.

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

                "This example of \n   Exception.Message, \n" +
                "   Exception.HelpLink, \n   Exception.Source, \n" +
                "   Exception.StackTrace, and \n   Exception." +
                "TargetSite \ngenerates the following output." );

                for( int count = 1; ; count++ )
                            "Log record number {0}", count ) );
            catch( Exception ex )
                Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
                    "\nHelpLink ---\n{0}", ex.HelpLink );
                Console.WriteLine( "\nSource ---\n{0}", ex.Source );
                    "\nStackTrace ---\n{0}", ex.StackTrace );
                    "\nTargetSite ---\n{0}", ex.TargetSite );

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_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()

TargetSite ---
Int32 AddRecord(System.String)

Universal Windows Platform
Available since 4.5
.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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft