Exception.StackTrace 속성

정의

호출 스택의 직접 실행 프레임 문자열 표현을 가져옵니다.

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

속성 값

호출 스택의 직접 실행 프레임을 설명하는 문자열입니다. 사용할 수 있는 스택 추적이 없는 경우(예: 문에서 throw 스택 해제 이전) 값은 입니다 null.

구현

예제

다음 코드 예제를 Exception throw 한 다음 catch 하 고 사용 하 여 스택 추적을 표시 합니다 StackTrace 속성입니다.

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

설명

실행 스택은 지정된 순간에 실행 중인 모든 메서드를 추적합니다. 메서드 호출의 추적을 스택 추적이라고 합니다. 스택 추적 목록은 예외가 발생하는 메서드의 줄 번호에 대한 호출 스택을 따르는 방법을 제공합니다.

속성은 StackTrace 예외가 throw된 위치에서 발생하는 호출 스택의 프레임을 반환합니다. 클래스의 새 instance 만들고 해당 StackTrace.ToString 메서드를 사용하여 호출 스택의 추가 프레임에 System.Diagnostics.StackTrace 대한 정보를 얻을 수 있습니다.

애플리케이션 코드에서 예외가 발생 될 때마다 스택 추적을 업데이트 하는 CLR (공용 언어 런타임) (사용 하 여는 throw 키워드). 예외가 원래 throw된 메서드와 다른 메서드에서 다시 throw된 경우 스택 추적에는 예외가 원래 throw된 메서드의 위치와 예외가 다시 throw된 메서드의 위치가 모두 포함됩니다. 예외가 throw되고 나중에 동일한 메서드에서 다시 throw되는 경우 스택 추적에는 예외가 다시 발생한 위치만 포함되며 예외가 원래 throw된 위치는 포함되지 않습니다.

속성은 StackTrace 최적화 중에 발생하는 인라인 처리와 같은 코드 변환으로 인해 예상대로 많은 메서드 호출을 보고하지 않을 수 있습니다.

상속자 참고

StackTrace 속성은 스택 추적 콘텐츠 또는 형식을 제어해야 하는 클래스에서 재정의됩니다.

기본적으로 스택 추적은 예외 개체가 throw되기 직전에 캡처됩니다. 예외가 throw되지 않을 때 를 사용하여 StackTrace 스택 추적 정보를 가져옵니다.

적용 대상

추가 정보