当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET StackTrace类代码示例

本文整理汇总了VB.NET中System.Diagnostics.StackTrace的典型用法代码示例。如果您正苦于以下问题:VB.NET StackTrace类的具体用法?VB.NET StackTrace怎么用?VB.NET StackTrace使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了StackTrace类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: StackTraceSample

' 导入命名空间
Imports System.Diagnostics

Class StackTraceSample
   
    <STAThread()>  _
    Public Shared Sub Main()

        Dim sample As New StackTraceSample()
        Try
   
            sample.MyPublicMethod()
        Catch
            ' Create a StackTrace that captures
            ' filename, line number, and column
            ' information for the current thread.
            Dim st As New StackTrace(True)
            Dim i As Integer

            For i = 0 To st.FrameCount - 1

                ' Note that high up the call stack, there is only
                ' one stack frame.
                Dim sf As StackFrame = st.GetFrame(i)
                Console.WriteLine()
                Console.WriteLine("High up the call stack, Method: {0}", _
                    sf.GetMethod())
            
                Console.WriteLine("High up the call stack, Line Number: {0}", _
                    sf.GetFileLineNumber())
            Next i
        End Try
    End Sub
   
   Public Sub MyPublicMethod()
      MyProtectedMethod()
   End Sub
    
   Protected Sub MyProtectedMethod()
      Dim mic As New MyInternalClass()
      mic.ThrowsException()
   End Sub
   
   
   Class MyInternalClass
      
      Public Sub ThrowsException()
         Try
            Throw New Exception("A problem was encountered.")
         Catch e As Exception

            ' Create a StackTrace that captures filename,
            ' line number and column information.
            Dim st As New StackTrace(True)

            Dim stackIndent As String = ""
            Dim i As Integer
            For i = 0 To st.FrameCount - 1
               ' Note that at this level, there are four
               ' stack frames, one for each method invocation.
               Dim sf As StackFrame = st.GetFrame(i)
               Console.WriteLine()
               Console.WriteLine(stackIndent + " Method: {0}", _
                   sf.GetMethod())
               Console.WriteLine(stackIndent + " File: {0}", _
                   sf.GetFileName())
               Console.WriteLine(stackIndent + " Line Number: {0}", _
                   sf.GetFileLineNumber())
               stackIndent += "  "
            Next i
            Throw e
         End Try
      End Sub
   End Class
End Class


' This console application produces the following output when
' compiled with the Debug configuration.
'   
'    Method: Void ThrowsException()
'    File: c:\pp\samples\stacktraceframe\myclass.vb
'    Line Number: 55
' 
'      Method: Void MyProtectedMethod()
'      File: c:\pp\samples\stacktraceframe\myclass.vb
'      Line Number: 42
' 
'        Method: Void MyPublicMethod()
'        File: c:\pp\samples\stacktraceframe\myclass.vb
'        Line Number: 37
' 
'          Method: Void Main(System.String[])
'          File: c:\pp\samples\stacktraceframe\myclass.vb
'          Line Number: 13
' 
'   High up the call stack, Method: Void Main(System.String[])
'   High up the call stack, Line Number: 18
' 
' 
' This console application produces the following output when
' compiled with the Release configuration.
' 
'    Method: Void ThrowsException()
'    File:
'    Line Number: 0
' 
'      Method: Void Main(System.String[])
'      File:
'      Line Number: 0
' 
'   High up the call stack, Method: Void Main()
'   High up the call stack, Line Number: 0
'
开发者ID:VB.NET开发者,项目名称:System.Diagnostics,代码行数:114,代码来源:StackTrace

示例2: New StackTrace

public class Test
   public Shared Sub Main
        CalculateEmployeeSalaries()
   End Sub
    Private Shared Sub CalculateEmployeeSalaries()
        CheckVacationPay()
    End Sub

    Private Shared Sub CheckVacationPay()
        ShowCallStack()
    End Sub


    Private Shared Sub ShowCallStack()
        Dim stack_trace As New System.Diagnostics.StackTrace(True)
        For i As Integer = 0 To stack_trace.FrameCount - 1
            With stack_trace.GetFrame(i)
                Console.WriteLine("Method: " & .GetMethod().ToString)
                Console.WriteLine("File: " & .GetFileName())
                Console.WriteLine("Line: " & .GetFileLineNumber())
            End With
        Next i

    End Sub

End class
开发者ID:VB程序员,项目名称:System.Diagnostics,代码行数:26,代码来源:StackTrace


注:本文中的System.Diagnostics.StackTrace类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。