StackFrame.ToString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立堆疊追蹤的可讀取表示。
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
傳回
堆疊追蹤的可讀取表示。
範例
下列範例示範 ToString 方法的用法。 此程式代碼範例是針對 類別提供的較大範例的 StackFrame 一部分。
void InternalMethod()
{
try
{
ClassLevel2^ nestedClass = gcnew ClassLevel2;
nestedClass->Level2Method();
}
catch ( Exception^ e )
{
Console::WriteLine( " InternalMethod exception handler" );
// Build a stack trace from one frame, skipping the
// current frame and using the next frame. By
// default, file and line information are not displayed.
StackTrace^ st = gcnew StackTrace( gcnew StackFrame( 1 ) );
Console::WriteLine( " Stack trace for next level frame: {0}", st->ToString() );
Console::WriteLine( " Stack frame for next level: " );
Console::WriteLine( " {0}", st->GetFrame( 0 )->ToString() );
Console::WriteLine( " Line Number: {0}", st->GetFrame( 0 )->GetFileLineNumber().ToString() );
Console::WriteLine();
Console::WriteLine( " ... throwing exception to next level ..." );
Console::WriteLine( "-------------------------------------------------\n" );
throw e;
}
}
public void InternalMethod()
{
try
{
ClassLevel2 nestedClass = new ClassLevel2();
nestedClass.Level2Method();
}
catch (Exception e)
{
Console.WriteLine(" InternalMethod exception handler");
// Build a stack trace from one frame, skipping the
// current frame and using the next frame. By
// default, file and line information are not displayed.
StackTrace st = new StackTrace(new StackFrame(1));
Console.WriteLine(" Stack trace for next level frame: {0}",
st.ToString());
Console.WriteLine(" Stack frame for next level: ");
Console.WriteLine(" {0}", st.GetFrame(0).ToString());
Console.WriteLine(" Line Number: {0}",
st.GetFrame(0).GetFileLineNumber().ToString());
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
Public Sub InternalMethod()
Try
Dim nestedClass As New ClassLevel2
nestedClass.Level2Method()
Catch e As Exception
Console.WriteLine(" InternalMethod exception handler")
' Build a stack trace from one frame, skipping the
' current frame and using the next frame. By default,
' file and line information are not displayed.
Dim st As New StackTrace(New StackFrame(1))
Console.WriteLine(" Stack trace for next level frame: {0}", _
st.ToString())
Console.WriteLine(" Stack frame for next level: ")
Console.WriteLine(" {0}", st.GetFrame(0).ToString())
Console.WriteLine(" Line Number: {0}", _
st.GetFrame(0).GetFileLineNumber().ToString())
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub