Exception.Message Właściwość

Definicja

Pobiera komunikat opisujący bieżący wyjątek.

C#
public virtual string Message { get; }

Wartość właściwości

Komunikat o błędzie wyjaśniający przyczynę wyjątku lub pusty ciąg ("").

Implementuje

Przykłady

Poniższy przykład kodu zgłasza wyjątek, a następnie przechwytuje Exception wyjątek i wyświetla komunikat tekstowy wyjątku Message przy użyciu właściwości .

C#
// 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 = "https://learn.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 ---
https://learn.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)
*/

Uwagi

Komunikaty o błędach są przeznaczone dla dewelopera, który obsługuje wyjątek. Tekst Message właściwości powinien całkowicie opisać błąd, a jeśli to możliwe, powinien również wyjaśnić, jak naprawić błąd. Programy obsługi wyjątków najwyższego poziomu mogą wyświetlać komunikat dla użytkowników końcowych, dlatego należy upewnić się, że jest on poprawnie gramatyczny i że każde zdanie komunikatu kończy się kropką. Nie używaj znaków zapytania ani wykrzykników. Jeśli aplikacja używa zlokalizowanych komunikatów o wyjątkach, upewnij się, że są one dokładnie przetłumaczone.

Ważne

Nie ujawniaj poufnych informacji w komunikatach o wyjątkach bez sprawdzania odpowiednich uprawnień.

Wartość Message właściwości jest uwzględniana w informacjach zwracanych przez ToStringelement . Właściwość Message jest ustawiana tylko podczas tworzenia obiektu Exception. Jeśli żaden komunikat nie został dostarczony do konstruktora dla bieżącego wystąpienia, system dostarcza domyślny komunikat sformatowany przy użyciu bieżącej kultury systemu.

Uwagi dotyczące dziedziczenia

Właściwość Message jest zastępowana w klasach, które wymagają kontroli nad zawartością lub formatem wiadomości. Kod aplikacji zwykle uzyskuje dostęp do tej właściwości, gdy musi wyświetlać informacje o wyjątku, który został przechwycony.

Komunikat o błędzie powinien być zlokalizowany.

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0