Exception.StackTrace 屬性

定義

取得呼叫堆疊上即時運算框架的字串表示。

C#
public virtual string StackTrace { get; }
C#
public virtual string? StackTrace { get; }

屬性值

字串,描述呼叫堆疊的立即框架。 如果沒有可用的堆疊追蹤 (,例如在從 throw 語句回溯之前) ,則值為 null

實作

範例

下列程式碼範例會 Exception 擲回 ,然後使用 屬性攔截它並顯示堆疊追蹤 StackTrace

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

備註

執行堆疊會持續追蹤指定的執行個體的所有執行中方法。 方法呼叫的追蹤稱為堆疊追蹤。 堆疊追蹤清單提供方法,以追蹤呼叫堆疊到發生例外狀況的方法中的行號。

屬性 StackTrace 會傳回呼叫堆疊的框架,該框架源自擲回例外狀況的位置。 您可以藉由建立 類別的新實例 System.Diagnostics.StackTrace 並使用其 StackTrace.ToString 方法,取得呼叫堆疊中其他框架的相關資訊。

每當應用程式程式碼 (擲回例外狀況時,Common Language Runtime (CLR) 會使用 throw 關鍵字) 來更新堆疊追蹤。 如果在方法中重新擲回例外狀況,其與原本擲回的方法不同,堆疊追蹤會同時包含方法中原本擲回例外狀況的位置,以及重新擲回例外狀況的方法中的位置。 如果擲回例外狀況,並在稍後重新擲回,則堆疊追蹤只會包含重新擲回例外狀況的位置,且不包含最初擲回例外狀況的位置。

屬性 StackTrace 可能不會如預期般回報許多方法呼叫,因為程式碼轉換,例如內嵌,會在優化期間發生。

給繼承者的注意事項

屬性 StackTrace 會在需要控制堆疊追蹤內容或格式的類別中覆寫。

根據預設,堆疊追蹤會在擲回例外狀況物件之前立即擷取。 當未擲回例外狀況時,請使用 StackTrace 來取得堆疊追蹤資訊。

適用於

產品 版本
.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

另請參閱