StackTrace.GetFrame(Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得指定的堆疊框架。
public:
virtual System::Diagnostics::StackFrame ^ GetFrame(int index);
public virtual System.Diagnostics.StackFrame? GetFrame (int index);
public virtual System.Diagnostics.StackFrame GetFrame (int index);
abstract member GetFrame : int -> System.Diagnostics.StackFrame
override this.GetFrame : int -> System.Diagnostics.StackFrame
Public Overridable Function GetFrame (index As Integer) As StackFrame
參數
- index
- Int32
要求的堆疊框架索引。
傳回
指定的堆疊框架。
範例
下列程式代碼範例會顯示堆疊追蹤中的第一個和最後一個函數調用。
void Level5Method()
{
try
{
ClassLevel6^ nestedClass = gcnew ClassLevel6;
nestedClass->Level6Method();
}
catch ( Exception^ e )
{
Console::WriteLine( " Level5Method exception handler" );
StackTrace^ st = gcnew StackTrace;
// Display the most recent function call.
StackFrame^ sf = st->GetFrame( 0 );
Console::WriteLine();
Console::WriteLine( " Exception in method: " );
Console::WriteLine( " {0}", sf->GetMethod() );
if ( st->FrameCount > 1 )
{
// Display the highest-level function call
// in the trace.
sf = st->GetFrame( st->FrameCount - 1 );
Console::WriteLine( " Original function call at top of call stack):" );
Console::WriteLine( " {0}", sf->GetMethod() );
}
Console::WriteLine();
Console::WriteLine( " ... throwing exception to next level ..." );
Console::WriteLine( "-------------------------------------------------\n" );
throw e;
}
}
public void Level5Method()
{
try
{
ClassLevel6 nestedClass = new ClassLevel6();
nestedClass.Level6Method();
}
catch (Exception e)
{
Console.WriteLine(" Level5Method exception handler");
StackTrace st = new StackTrace();
// Display the most recent function call.
StackFrame sf = st.GetFrame(0);
Console.WriteLine();
Console.WriteLine(" Exception in method: ");
Console.WriteLine(" {0}", sf.GetMethod());
if (st.FrameCount >1)
{
// Display the highest-level function call
// in the trace.
sf = st.GetFrame(st.FrameCount-1);
Console.WriteLine(" Original function call at top of call stack):");
Console.WriteLine(" {0}", sf.GetMethod());
}
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
Public Sub Level5Method()
Try
Dim nestedClass As New ClassLevel6()
nestedClass.Level6Method()
Catch e As Exception
Console.WriteLine(" Level5Method exception handler")
Dim st As New StackTrace()
' Display the most recent function call.
Dim sf As StackFrame = st.GetFrame(0)
Console.WriteLine()
Console.WriteLine(" Exception in method: ")
Console.WriteLine(" {0}", sf.GetMethod())
If st.FrameCount > 1 Then
' Display the highest-level function call in the trace.
sf = st.GetFrame((st.FrameCount - 1))
Console.WriteLine(" Original function call at top of call stack):")
Console.WriteLine(" {0}", sf.GetMethod())
End If
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
備註
堆疊框架的編號是從 0 開始,這是推送的最後一個堆疊框架。