[.NET] CErrStack 方便地管理错误或异常

Option Explicit On
Option Strict On

Imports System.Reflection
Imports System.Diagnostics


Public Structure ErrInfo

    Public Number As Integer
    Public Description As String
    Public Comment As String

    Public ClassName As String
    Public FuncName As String

    Public Sub New(Number As Integer, Description As String, Comment As String, ClassName As String, FuncName As String)
        With Me
            .Number = Number
            .Description = Description
            .Comment = Comment

            .ClassName = ClassName
            .FuncName = FuncName
        End With
    End Sub

    Public Overrides Function ToString() As String
        Dim Msg As String
        If Comment IsNot Nothing Then
            Msg = ClassName & "::" & FuncName & "()" & vbCrLf & _
                  "Err " & Number.ToString & ": " & Description & vbCrLf & _
                  vbCrLf & _
                  Comment
        Else
            Msg = ClassName & "::" & FuncName & "()" & vbCrLf & _
                  "Err " & Number.ToString & ": " & Description
        End If
        Return Msg
    End Function

    Public Sub ShowMsgBox()
        MsgBox(Me.ToString(), MsgBoxStyle.Critical, "Error " & Number.ToString)
    End Sub

End Structure



Public Class CErrStack

    Private m_ErrStack As New Stack(Of ErrInfo)

    Public Function Push(ErrInfo As ErrInfo) As ErrInfo
        m_ErrStack.Push(ErrInfo)
        Return ErrInfo
    End Function


    Public Function Push(Optional Comment As String = Nothing, Optional StackFrameIndex As Integer = 1) As ErrInfo
        Dim ErrObj As ErrObject = Err()
        Return Push(ErrObj.Number, ErrObj.Description, Comment, StackFrameIndex + 1)
    End Function


    Public Function Push(Number As Integer, Description As String, Optional Comment As String = Nothing, Optional StackFrameIndex As Integer = 1) As ErrInfo
        Dim STrace As New StackTrace(True)
        Dim SFrame As StackFrame = STrace.GetFrame(StackFrameIndex)
        Dim tError As ErrInfo
        If SFrame Is Nothing Then
            tError = New ErrInfo(Number, Description, Comment, "UnknownClass", "UnknownMethod")
        Else
            Dim tMethod As MethodBase = SFrame.GetMethod()
            tError = New ErrInfo(Number, Description, Comment, tMethod.ReflectedType.FullName, tMethod.Name)
        End If
        m_ErrStack.Push(tError)
        Return tError
    End Function


    Public Function Push(Err As ErrObject, Optional Comment As String = Nothing, Optional StackFrameIndex As Integer = 1) As ErrInfo
        Return Push(Err.Number, Err.Description, Comment, StackFrameIndex + 1)
    End Function


    Public Function Push(Ex As Exception, Optional Comment As String = Nothing) As ErrInfo
        m_ErrStack.Push(New ErrInfo(Err().Number,
                                        Ex.Message,
                                        Comment,
                                        Ex.TargetSite.ReflectedType.FullName,
                                        Ex.TargetSite.Name))
        Return m_ErrStack.Last()
    End Function

    Public Function Push(Ex As Exception, Number As Integer, Optional Comment As String = Nothing) As ErrInfo
        m_ErrStack.Push(New ErrInfo(Number,
                                        Ex.Message,
                                        Comment,
                                        Ex.TargetSite.ReflectedType.FullName,
                                        Ex.TargetSite.Name))
        Return m_ErrStack.Last()
    End Function


    Public Function Pop() As ErrInfo
        If m_ErrStack.Count = 0 Then Return Nothing
        Return m_ErrStack.Pop()
    End Function


    Public ReadOnly Property Last() As ErrInfo
        Get
            If m_ErrStack.Count = 0 Then Return Nothing
            Return m_ErrStack.Last()
        End Get
    End Property


    Public Function MsgLastErrPop() As ErrInfo
        If m_ErrStack.Count = 0 Then Return Nothing
        MsgLastErrPop = m_ErrStack.Pop()
        MsgLastErrPop.ShowMsgBox()
    End Function


    Public Function MsgLastErr() As ErrInfo
        If m_ErrStack.Count = 0 Then Return Nothing
        MsgLastErr = m_ErrStack.Peek()
        MsgLastErr.ShowMsgBox()
    End Function


    Public Overrides Function ToString() As String
        If m_ErrStack.Count = 0 Then Return Nothing
        Return m_ErrStack.Last().ToString()
    End Function

End Class

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。