Share via


Exception.StackTrace 属性

获取当前异常发生时调用堆栈上的帧的字符串表示形式。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable ReadOnly Property StackTrace As String
用法
Dim instance As Exception
Dim value As String

value = instance.StackTrace
public virtual string StackTrace { get; }
public:
virtual property String^ StackTrace {
    String^ get ();
}
/** @property */
public String get_StackTrace ()
public function get StackTrace () : String

属性值

一个字符串,它描述调用堆栈的内容,其中首先显示最近的方法调用。

备注

执行堆栈跟踪在给定时刻正在执行的所有方法。对方法调用的跟踪称为堆栈跟踪。堆栈跟踪列表提供了一种循着调用序列跟踪到方法中异常发生处行号的手段。

由于优化期间发生代码转换(如内联),因此 StackTrace 可能无法像预期的那样报告很多方法调用。

给继承者的说明 在需要控制堆栈跟踪内容或格式的类中重写 StackTrace 属性。 默认情况下,在引发异常对象前一刻捕获堆栈跟踪。在没有发生异常时,可以使用 Environment.StackTrace 来获取堆栈跟踪信息。

示例

下面的代码示例引发一个 Exception,然后捕捉该异常,并使用 StackTrace 属性显示堆栈跟踪。

' 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)
// 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)
*/
// 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://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 ---
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)
*/
// Example for the Exception.HelpLink, Exception.Source,
// Exception.StackTrace, and Exception.TargetSite properties.
package NDP_UE_JSL; 

import System.* ;

// Derive an exception; the constructor sets the HelpLink and 
// Source properties.
class LogTableOverflowException extends System.Exception
{
    private String overflowMessage = "The log table has overflowed.";

    public LogTableOverflowException(System.String auxMessage, 
        System.Exception inner)
    {
        super(String.Format("The log table has overflowed. - {0}", 
            auxMessage), inner);
        this.set_HelpLink("https://msdn.microsoft.com");
        this.set_Source("Exception_Class_Samples");
    } //LogTableOverflowException
} //LogTableOverflowException

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

    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) throws LogTableOverflowException
    {
        try {
            logArea.set_Item(elemInUse, newRecord);
            return elemInUse++;
        }
        catch (System.Exception e) {
            throw new LogTableOverflowException(
                String.Format("Record \"{0}\" was not logged.", newRecord), e);
        }
    } //AddRecord
} //LogTable

class OverflowDemo
{
    // Create a log table and force an overflow.
    public static void main(String[] args)
    {
        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}", 
                    System.Convert.ToString(count)));
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine("\nMessage ---\n{0}", ex.get_Message());
            Console.WriteLine("\nHelpLink ---\n{0}", ex.get_HelpLink());
            Console.WriteLine("\nSource ---\n{0}", ex.get_Source());
            Console.WriteLine("\nStackTrace ---\n{0}", ex.get_StackTrace());
            Console.WriteLine("\nTargetSite ---\n{0}", ex.get_TargetSite());
        }
    } //main
} //OverflowDemo
   
/*
   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_JSL.LogTable.AddRecord(String newRecord) in D:\Test\ConsoleApplicat
ion7\ConsoleApplication7\Class1.jsl:line 45
   at NDP_UE_JSL.OverflowDemo.main(String[] args) in D:\Test\ConsoleApplication7
\ConsoleApplication7\Class1.jsl:line 62

TargetSite ---
Int32 AddRecord(System.String)
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Exception 类
Exception 成员
System 命名空间
Environment.StackTrace 属性