Exception.StackTrace Exception.StackTrace Exception.StackTrace Exception.StackTrace Property

정의

호출 스택의 직접 실행 프레임 문자열 표현을 가져옵니다. Gets a string representation of the immediate frames on the call stack.

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

호출 스택의 직접 실행 프레임을 설명하는 문자열입니다. A string that describes the immediate frames of the call stack.

구현

예제

다음 코드 예제에서는 throw를 Exception 한 후 catch 하 여 스택 추적을 표시 합니다 StackTrace 속성입니다.The following code example throws an Exception and then catches it and displays a stack trace using the StackTrace property.

// 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)
*/
// 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.
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)

설명

실행 스택은 특정 시점에 실행 중인 모든 메서드를 추적합니다.The execution stack keeps track of all the methods that are in execution at a given instant. 메서드 호출을 추적하는 것을 스택 추적이라고 합니다.A trace of the method calls is called a stack trace. 스택 추적 목록은 예외가 발생 메서드의 줄 번호로 호출 스택을 따를 수가 있습니다.The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs.

StackTrace 속성은 예외가 throw 된 위치에서 발생 하는 호출 스택의 프레임을 반환 합니다.The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown. 새 인스턴스를 만들어 호출 스택에서 추가 프레임에 대 한 정보를 얻을 수 있습니다 합니다 System.Diagnostics.StackTrace 클래스 및 사용 하 여 해당 StackTrace.ToString 메서드.You can obtain information about additional frames in the call stack by creating a new instance of the System.Diagnostics.StackTrace class and using its StackTrace.ToString method.

응용 프로그램 코드에서 예외가 발생 될 때마다 스택 추적을 업데이트 하는 CLR (공용 언어 런타임) (사용 하 여는 throw 키워드).The common language runtime (CLR) updates the stack trace whenever an exception is thrown in application code (by using the throw keyword). 예외는 다른 방법 보다 원래 발생 하는 메서드에서 throw 된, 스택 추적 있으면 여기서 예외가 throw 된 원래, 메서드에서 위치와 메서드에서 예외가 있는 위치 다시 throw 합니다.If the exception was rethrown in a method that is different than the method where it was originally thrown, the stack trace contains both the location in the method where the exception was originally thrown, and the location in the method where the exception was rethrown. 예외는 throw 되 고 나중에 다시 throw를 동일한 메서드에서 스택 추적은 예외가 다시 throw 된 예외 및 예외가 throw 된 원래 위치에 없는 위치만 포함 됩니다.If the exception is thrown, and later rethrown, in the same method, the stack trace only contains the location where the exception was rethrown and does not include the location where the exception was originally thrown.

StackTrace 속성 수와 같은 코드 변환으로 인해 예상 만큼 많은 메서드 호출을 보고 하지 인라인 처리를 최적화 하는 동안 발생 하는 합니다.The StackTrace property may not report as many method calls as expected because of code transformations, such as inlining, that occur during optimization.

상속자 참고

StackTrace 속성 스택 추적 내용 또는 형식이 제어할 필요가 있는 클래스에서 재정의 됩니다. The StackTrace property is overridden in classes that require control over the stack trace content or format. 기본적으로 스택 추적을 예외 개체가 throw 되기 직전 캡처됩니다. By default, the stack trace is captured immediately before an exception object is thrown. 사용 하 여 StackTrace 예외가 throw 되는 경우 스택 추적 정보를 가져오려고 합니다. Use StackTrace to get stack trace information when no exception is being thrown.

적용 대상

추가 정보