次の方法で共有


Exception.Message プロパティ

現在の例外を説明するメッセージを取得します。

Public Overridable ReadOnly Property Message As String
[C#]
public virtual string Message {get;}
[C++]
public: __property virtual String* get_Message();
[JScript]
public function get Message() : String;

プロパティ値

例外の理由を説明するエラー メッセージ、または空の文字列 ("")。

解説

Message にはエラーについて詳しく説明するテキストを指定する必要があり、可能な場合は、そのエラーの対処方法についても明記したテキストを指定します。Message プロパティの値は、 ToString により返される情報に含まれています。

Message プロパティは、 Exception の作成時にだけ設定されます。現在のインスタンスのコンストラクタにメッセージが渡されなかった場合は、現在のシステムのカルチャを使用して書式化した既定のメッセージが渡されます。

継承時の注意: Message プロパティは、メッセージの内容または書式に対する制御が必要なクラスではオーバーライドされます。受け取った例外に関する情報を表示する必要がある場合、アプリケーション コードは通常、このプロパティにアクセスします。

エラー メッセージはローカライズする必要があります。

使用例

[Visual Basic, C#, C++] Exception をスローしてキャッチし、 Message プロパティを使用してその例外のテキスト メッセージを表示するコード例を次に示します。

 
' 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 = "https://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 ---
' https://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)

[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://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 ---
https://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)
*/

[C++] 
// Example for the Exception::HelpLink, Exception::Source,
// Exception::StackTrace, and Exception::TargetSite properties.
#using <mscorlib.dll>
using namespace System;

namespace NDP_UE_CPP
{
    // Derive an exception; the constructor sets the HelpLink and 
    // Source properties.
    public __gc class LogTableOverflowException : public Exception
    {
        static String* overflowMessage = 
            S"The log table has overflowed.";

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

    public __gc class LogTable
    {
    public: 
        LogTable( int numElements )
        {
            logArea = new String*[ numElements ];
            elemInUse = 0;
        }

    protected: 
        String*   logArea[ ];
        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* ex )
            {
                throw new LogTableOverflowException( 
                    String::Format( 
                        S"Record \"{0}\" was not logged.", 
                            newRecord ), ex );
            }
        }
    };

    // Create a log table and force an overflow.
    void ForceOverflow( )
    {
        LogTable* log = new LogTable( 4 );

        try
        {
            for( int count = 1; ; count++ )
            {
                log->AddRecord( 
                    String::Format( 
                        S"Log record number {0}", __box(count) ) );
            }
        }
        catch( Exception* ex )
        {
            Console::WriteLine( S"\nMessage ---\n{0}", ex->Message );
            Console::WriteLine( 
                S"\nHelpLink ---\n{0}", ex->HelpLink );
            Console::WriteLine( S"\nSource ---\n{0}", ex->Source );
            Console::WriteLine( 
                S"\nStackTrace ---\n{0}", ex->StackTrace );
            Console::WriteLine( S"\nTargetSite ---\n{0}", 
                ex->TargetSite->ToString( ) );
        }
    }
}

void main() 
{
    Console::WriteLine( 
        S"This example of \n   Exception::Message, \n"
        S"   Exception::HelpLink, \n   Exception::Source, \n"
        S"   Exception::StackTrace, and \n   Exception::" 
        S"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://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)
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Exception クラス | Exception メンバ | System 名前空間