码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-03-01 22:17:09      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/catchyrime/p/4307578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!