Exception.Message 속성

정의

현재 예외를 설명하는 메시지를 가져옵니다.

public:
 virtual property System::String ^ Message { System::String ^ get(); };
public virtual string Message { get; }
member this.Message : string
Public Overridable ReadOnly Property Message As String

속성 값

예외의 원인을 설명하는 오류 메시지이거나 빈 문자열("")입니다.

구현

예제

다음 코드 예제를 throw 하 고 예외를 Exception catch 하 고 사용 하 여 예외의 문자 메시지를 표시 합니다 Message 속성입니다.

// 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 = "https://docs.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 ---
https://docs.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)
*/
// 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://docs.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://docs.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.
open System
        
let overflowMessage = "The log table has overflowed."

// Derive an exception; the constructor sets the HelpLink and
// Source properties.
type LogTableOverflowException(auxMessage, inner) as this =
    inherit Exception($"%s{overflowMessage} - %s{auxMessage}", inner)

    do
        this.HelpLink <- "https://docs.microsoft.com"
        this.Source <- "Exception_Class_Samples"

type LogTable(numElements) =
    let logArea = Array.zeroCreate<string> numElements
    let mutable elemInUse = 0

    // The AddRecord method throws a derived exception if
    // the array bounds exception is caught.
    member this.AddRecord(newRecord) =
        try
            logArea[elemInUse] <- newRecord
            elemInUse <- elemInUse + 1
            elemInUse - 1 
        with e ->
            raise (LogTableOverflowException($"Record \"{newRecord}\" was not logged.", e) )

// Create a log table and force an overflow.
let log = LogTable 4 

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

try
    for count = 1 to 1000000 do
        log.AddRecord $"Log record number {count}"
        |> ignore
with ex ->
    printfn $"\nMessage ---\n{ex.Message}"
    printfn $"\nHelpLink ---\n{ex.HelpLink}"
    printfn $"\nSource ---\n{ex.Source}"
    printfn $"\nStackTrace ---\n{ex.StackTrace}"
    printfn $"\nTargetSite ---\n{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://docs.microsoft.com

// Source ---
// Exception_Class_Samples

// StackTrace ---
//    at NDP_UE_FS.LogTable.AddRecord(String newRecord)
//    at <StartupCode$fs>.$NDP_UE_FS.main@()

// TargetSite ---
// Int32 AddRecord(System.String)
' Example for the Exception.HelpLink, Exception.Source,
' Exception.StackTrace, and Exception.TargetSite properties.
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 = "https://docs.microsoft.com"
            Me.Source = "Exception_Class_Samples"

        End Sub
    End Class

    Class LogTable
       
        Public Sub New(numElements As Integer)
            logArea = New String(numElements) {}
            elemInUse = 0
        End Sub
           
        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

    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

    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 ---
' https://docs.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)

설명

오류 메시지는 예외를 처리하는 개발자를 대상으로 합니다. 속성의 Message 텍스트는 오류를 완전히 설명해야 하며 가능한 경우 오류를 수정하는 방법도 설명해야 합니다. 최상위 예외 처리기는 최종 사용자에게 메시지를 표시할 수 있으므로 문법적으로 올바르고 메시지의 각 문장이 마침표로 끝나는지 확인해야 합니다. 물음표나 느낌표를 사용하지 마세요. 애플리케이션에서 지역화 된 예외 메시지를 사용 하는 경우 정확 하 게 변환 하는 것이 해야 합니다.

중요

적절한 권한을 확인하지 않고 예외 메시지에 중요한 정보를 공개하지 마세요.

속성 값 Message 은 에서 반환 ToString하는 정보에 포함됩니다. 속성은 Message 를 만들 때만 설정됩니다 Exception. 현재 instance 대한 생성자에 메시지가 제공되지 않은 경우 시스템은 현재 시스템 문화권을 사용하여 형식이 지정된 기본 메시지를 제공합니다.

상속자 참고

속성은 Message 메시지 콘텐츠 또는 형식을 제어해야 하는 클래스에서 재정의됩니다. 일반적으로 애플리케이션 코드는 예외 발생 했습니다에 대 한 정보를 표시 해야 할 경우이 속성을 액세스 합니다.

오류 메시지는 지역화되어야 합니다.

적용 대상