自定义异常类的使用...

自定义异常类的使用

-首先定义三个基本类: UErrorMessage.vb ,  UErrorMessageCollection.vb , UException.vb 

-一个Form:  Form_UException.vb

 

UErrorMessage.vb

Namespace Exceptions

    Public Class UErrorMessage

        Private istg_error_code As String

        Private istg_error_cdesc As String

        Private istg_error_edesc As String

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_cdesc As String, ByVal astg_error_edesc As String)

            SetErrorCode(astg_error_code)

            SetErrorDescription(New System.Globalization.CultureInfo("zh-CN"), astg_error_cdesc)

            SetErrorDescription(New System.Globalization.CultureInfo("en-US"), astg_error_edesc)

        End Sub

 

 

        Private Sub SetErrorCode(ByVal astg_error_code As String)

            istg_error_code = astg_error_code

        End Sub

 

        Private Sub SetErrorDescription(ByRef acul_culture_info As System.Globalization.CultureInfo, ByVal astg_error_desc As String)

            If acul_culture_info.Name = "zh-CN" Then

                istg_error_cdesc = astg_error_desc

            ElseIf acul_culture_info.Name = "en-US" Then

                istg_error_edesc = astg_error_desc

            End If

        End Sub

 

        Public Sub FillErrorDescription(ByVal astg_replaced_string As String, ByVal astg_replaced_by_string As String)

            istg_error_cdesc.Replace(astg_replaced_string, astg_replaced_by_string)

            istg_error_edesc.Replace(astg_replaced_string, astg_replaced_by_string)

        End Sub

 

        Public Function GetErrorCode() As String

            GetErrorCode = istg_error_code

        End Function

 

        Public Overloads Function GetErrorDescription(ByRef acul_culture_info As System.Globalization.CultureInfo) As String

            Dim lstg_error_description As String

            If acul_culture_info.Name = "zh-CN" Or acul_culture_info.Name = "zh-CHT" Then

                lstg_error_description = istg_error_cdesc

            ElseIf acul_culture_info.Name = "en-US" Then

                lstg_error_description = istg_error_edesc

            End If

            GetErrorDescription = lstg_error_description

        End Function

 

    End Class

End Namespace

 

 

UErrorMessageCollection.vb

Namespace Exceptions

    Public Class UErrorMessageCollection

        Inherits System.Collections.CollectionBase

 

        Public Sub Add(ByVal apmr_parameter As UErrorMessage)

            List.Add(apmr_parameter)

        End Sub

 

        Public Sub Remove(ByVal aint_index As Integer)

            If aint_index > (Count - 1) Or aint_index < 0 Then

                ' If no widget exists, a messagebox is shown and the operation is

                ' cancelled.

                '                System.Windows.Forms.MessageBox.Show("Index not valid!")

            Else

                List.RemoveAt(aint_index)

            End If

        End Sub

 

        Public ReadOnly Property Item(ByVal aint_index As Integer) As UErrorMessage

            Get

                Return CType(List.Item(aint_index), UErrorMessage)

            End Get

        End Property

 

        Public Function GetItemIndex(ByVal astg_error_code As String) As Integer

            Dim lint_index As Integer

            Dim lboo_continue As Boolean

 

            lboo_continue = True

            lint_index = 0

            Do While lboo_continue = True

                If CType(List.Item(lint_index), UErrorMessage).GetErrorCode() = astg_error_code Then

                    lboo_continue = False

                Else

                    lint_index = lint_index + 1

                    If lint_index > (Count - 1) Then

                        lboo_continue = False

                        lint_index = -1

                    End If

                End If

            Loop

            GetItemIndex = lint_index

        End Function

 

    End Class

End Namespace

 

UException.vb

Namespace Exceptions

 

    Public MustInherit Class UException

        Inherits System.Exception

        Protected ierm_error_msg_list As UErrorMessageCollection

        Protected istg_error_code As String

        Protected iexp_current_inner_exception As Exception

        Private istg_custom_msg_1 As String

        Private istg_custom_msg_2 As String

        Private istg_custom_msg_3 As String

        Private istg_custom_msg_4 As String

        Private istg_custom_msg_5 As String

        Private istg_custom_msg_6 As String

 

        Public MustOverride Sub Initialize()

 

        Public Sub New(ByVal astg_error_code As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = ""

            istg_custom_msg_2 = ""

            istg_custom_msg_3 = ""

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_custom_msg_1 As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = astg_error_custom_msg_1

            istg_custom_msg_2 = ""

            istg_custom_msg_3 = ""

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_custom_msg_1 As String, ByVal astg_error_custom_msg_2 As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = astg_error_custom_msg_1

            istg_custom_msg_2 = astg_error_custom_msg_2

            istg_custom_msg_3 = ""

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_custom_msg_1 As String, ByVal astg_error_custom_msg_2 As String, ByVal astg_error_custom_msg_3 As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = astg_error_custom_msg_1

            istg_custom_msg_2 = astg_error_custom_msg_2

            istg_custom_msg_3 = astg_error_custom_msg_3

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_custom_msg_1 As String, ByVal astg_error_custom_msg_2 As String, ByVal astg_error_custom_msg_3 As String, ByVal astg_error_custom_msg_4 As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = astg_error_custom_msg_1

            istg_custom_msg_2 = astg_error_custom_msg_2

            istg_custom_msg_3 = astg_error_custom_msg_3

            istg_custom_msg_4 = astg_error_custom_msg_4

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_custom_msg_1 As String, ByVal astg_error_custom_msg_2 As String, ByVal astg_error_custom_msg_3 As String, ByVal astg_error_custom_msg_4 As String, ByVal astg_error_custom_msg_5 As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = astg_error_custom_msg_1

            istg_custom_msg_2 = astg_error_custom_msg_2

            istg_custom_msg_3 = astg_error_custom_msg_3

            istg_custom_msg_4 = astg_error_custom_msg_4

            istg_custom_msg_5 = astg_error_custom_msg_5

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_custom_msg_1 As String, ByVal astg_error_custom_msg_2 As String, ByVal astg_error_custom_msg_3 As String, ByVal astg_error_custom_msg_4 As String, ByVal astg_error_custom_msg_5 As String, ByVal astg_error_custom_msg_6 As String)

            MyBase.New(astg_error_code)

            istg_custom_msg_1 = astg_error_custom_msg_1

            istg_custom_msg_2 = astg_error_custom_msg_2

            istg_custom_msg_3 = astg_error_custom_msg_3

            istg_custom_msg_4 = astg_error_custom_msg_4

            istg_custom_msg_5 = astg_error_custom_msg_5

            istg_custom_msg_6 = astg_error_custom_msg_6

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = ""

            istg_custom_msg_2 = ""

            istg_custom_msg_3 = ""

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_custom_msg_1 As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = astg_custom_msg_1

            istg_custom_msg_2 = ""

            istg_custom_msg_3 = ""

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_custom_msg_1 As String, ByVal astg_custom_msg_2 As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = astg_custom_msg_1

            istg_custom_msg_2 = astg_custom_msg_2

            istg_custom_msg_3 = ""

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_custom_msg_1 As String, ByVal astg_custom_msg_2 As String, ByVal astg_custom_msg_3 As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = astg_custom_msg_1

            istg_custom_msg_2 = astg_custom_msg_2

            istg_custom_msg_3 = astg_custom_msg_3

            istg_custom_msg_4 = ""

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_custom_msg_1 As String, ByVal astg_custom_msg_2 As String, ByVal astg_custom_msg_3 As String, ByVal astg_custom_msg_4 As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = astg_custom_msg_1

            istg_custom_msg_2 = astg_custom_msg_2

            istg_custom_msg_3 = astg_custom_msg_3

            istg_custom_msg_4 = astg_custom_msg_4

            istg_custom_msg_5 = ""

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_custom_msg_1 As String, ByVal astg_custom_msg_2 As String, ByVal astg_custom_msg_3 As String, ByVal astg_custom_msg_4 As String, ByVal astg_custom_msg_5 As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = astg_custom_msg_1

            istg_custom_msg_2 = astg_custom_msg_2

            istg_custom_msg_3 = astg_custom_msg_3

            istg_custom_msg_4 = astg_custom_msg_4

            istg_custom_msg_5 = astg_custom_msg_5

            istg_custom_msg_6 = ""

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_custom_msg_1 As String, ByVal astg_custom_msg_2 As String, ByVal astg_custom_msg_3 As String, ByVal astg_custom_msg_4 As String, ByVal astg_custom_msg_5 As String, ByVal astg_custom_msg_6 As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

            istg_custom_msg_1 = astg_custom_msg_1

            istg_custom_msg_2 = astg_custom_msg_2

            istg_custom_msg_3 = astg_custom_msg_3

            istg_custom_msg_4 = astg_custom_msg_4

            istg_custom_msg_5 = astg_custom_msg_5

            istg_custom_msg_6 = astg_custom_msg_6

            SetErrorCode(astg_error_code)

            Initialize()

        End Sub

 

        Public Overloads Function GetErrorDescription() As String

            Dim lstg_error_description As String

            lstg_error_description = Me.GetErrorDescription(System.Threading.Thread.CurrentThread.CurrentUICulture)

            Return lstg_error_description

        End Function

 

        Public Overloads Function GetErrorDescription(ByRef acul_culture_info As System.Globalization.CultureInfo) As String

            Dim lstg_error_description As String

            lstg_error_description = ierm_error_msg_list.Item(ierm_error_msg_list.GetItemIndex(istg_error_code)).GetErrorDescription(acul_culture_info)

            lstg_error_description = lstg_error_description.Replace("%1", istg_custom_msg_1)

            lstg_error_description = lstg_error_description.Replace("%2", istg_custom_msg_2)

            lstg_error_description = lstg_error_description.Replace("%3", istg_custom_msg_3)

            lstg_error_description = lstg_error_description.Replace("%4", istg_custom_msg_4)

            lstg_error_description = lstg_error_description.Replace("%5", istg_custom_msg_5)

            lstg_error_description = lstg_error_description.Replace("%6", istg_custom_msg_6)

            Return lstg_error_description

        End Function

 

        Private Overloads Function GetInnerErrorDescription(ByRef acul_culture_info As System.Globalization.CultureInfo) As String

            If Not MyBase.InnerException Is Nothing Then

                If TypeOf MyBase.InnerException Is System.Data.OleDb.OleDbException Then

                    Return CType(MyBase.InnerException, System.Data.OleDb.OleDbException).Message

                ElseIf TypeOf Me.InnerException Is Exceptions.UException Then

                    Return CType(MyBase.InnerException, UException).GetErrorDescription(acul_culture_info)

                Else

                End If

            End If

        End Function

 

        Public Function GetAllErrorDescription() As String

            Return MyClass.GetAllErrorDescription(System.Threading.Thread.CurrentThread.CurrentUICulture)

        End Function

 

        Public Overridable Function GetAllErrorDescription(ByRef acul_culture_info As System.Globalization.CultureInfo) As String

            Dim lstg_inner_exception_description As String

 

            If Not MyBase.InnerException Is Nothing Then

                If TypeOf MyBase.InnerException Is System.Data.OleDb.OleDbException Then

                    lstg_inner_exception_description = Chr(13) + Chr(10) + "---------------------------------" + Chr(13) + Chr(10) + CType(MyBase.InnerException, System.Data.OleDb.OleDbException).Message

                Else

                    'lstg_inner_exception_description = ""

                    lstg_inner_exception_description = Chr(13) + Chr(10) + "---------------------------------" + Chr(13) + Chr(10) + CType(MyBase.InnerException, UException).GetErrorDescription(acul_culture_info)

                End If

            End If

            GetAllErrorDescription = istg_error_code + Chr(13) + Chr(10) + Chr(13) + Chr(10) + Me.GetErrorDescription(acul_culture_info) + lstg_inner_exception_description

        End Function

 

        Public Overloads Sub ShowErrorDescription()

            ShowErrorDescription("", System.Threading.Thread.CurrentThread.CurrentUICulture)

        End Sub

 

        Public Overloads Sub ShowErrorDescription(ByVal astg_msgbox_title As String)

            ShowErrorDescription(astg_msgbox_title, System.Threading.Thread.CurrentThread.CurrentUICulture)

        End Sub

 

        Public Overloads Sub ShowErrorDescription(ByVal astg_msgbox_title As String, ByVal acul_culture_info As System.Globalization.CultureInfo)

            'MessageBox.Show(GetErrorDescription(acul_culture_info), astg_msgbox_title)

            Dim lfrm_form As New Forms.Form_UException

            lfrm_form.SetException(Me)

            lfrm_form.ShowDialog()

        End Sub

 

        Private Sub SetErrorCode(ByVal astg_error_code As String)

            istg_error_code = astg_error_code

        End Sub

 

        Public Function GetErrorCode() As String

            GetErrorCode = istg_error_code

        End Function

 

    End Class

End Namespace

 

Form_UException.vb

Namespace Exceptions.Forms

    Public Class Form_UException

        Inherits System.Windows.Forms.Form

 

#Region " Windows Form Designer generated code "

 

        Public Sub New()

            MyBase.New()

 

            'This call is required by the Windows Form Designer.

            InitializeComponent()

 

            'Add any initialization after the InitializeComponent() call

 

        End Sub

 

        'Form overrides dispose to clean up the component list.

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

            If disposing Then

                If Not (components Is Nothing) Then

                    components.Dispose()

                End If

            End If

            MyBase.Dispose(disposing)

        End Sub

 

        'Required by the Windows Form Designer

        Private components As System.ComponentModel.IContainer

 

        'NOTE: The following procedure is required by the Windows Form Designer

        'It can be modified using the Windows Form Designer. 

        'Do not modify it using the code editor.

        Friend WithEvents btn_ok As System.Windows.Forms.Button

        Friend WithEvents btn_details As System.Windows.Forms.Button

        Friend WithEvents lbl_exception_code As System.Windows.Forms.Label

        Friend WithEvents gbx_details As System.Windows.Forms.GroupBox

        Friend WithEvents lbl_inner_exception_code As System.Windows.Forms.Label

        Friend WithEvents btn_prev As System.Windows.Forms.Button

        Friend WithEvents btn_next As System.Windows.Forms.Button

        Friend WithEvents btn_trace As System.Windows.Forms.Button

        Friend WithEvents rtb_exception_desc As System.Windows.Forms.RichTextBox

        Friend WithEvents rtb_inner_exception_desc As System.Windows.Forms.RichTextBox

        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

            Me.btn_ok = New System.Windows.Forms.Button

            Me.btn_details = New System.Windows.Forms.Button

            Me.lbl_exception_code = New System.Windows.Forms.Label

            Me.gbx_details = New System.Windows.Forms.GroupBox

            Me.rtb_inner_exception_desc = New System.Windows.Forms.RichTextBox

            Me.btn_trace = New System.Windows.Forms.Button

            Me.btn_next = New System.Windows.Forms.Button

            Me.btn_prev = New System.Windows.Forms.Button

            Me.lbl_inner_exception_code = New System.Windows.Forms.Label

            Me.rtb_exception_desc = New System.Windows.Forms.RichTextBox

            Me.gbx_details.SuspendLayout()

            Me.SuspendLayout()

            '

            'btn_ok

            '

            Me.btn_ok.Location = New System.Drawing.Point(424, 88)

            Me.btn_ok.Name = "btn_ok"

            Me.btn_ok.Size = New System.Drawing.Size(100, 22)

            Me.btn_ok.TabIndex = 1

            Me.btn_ok.Text = "确定(OK)"

            '

            'btn_details

            '

            Me.btn_details.Location = New System.Drawing.Point(320, 88)

            Me.btn_details.Name = "btn_details"

            Me.btn_details.Size = New System.Drawing.Size(100, 22)

            Me.btn_details.TabIndex = 3

            Me.btn_details.Text = "详细(Details)"

            '

            'lbl_exception_code

            '

            Me.lbl_exception_code.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

            Me.lbl_exception_code.Font = New System.Drawing.Font("Tahoma", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

            Me.lbl_exception_code.ForeColor = System.Drawing.Color.Red

            Me.lbl_exception_code.Location = New System.Drawing.Point(6, 4)

            Me.lbl_exception_code.Name = "lbl_exception_code"

            Me.lbl_exception_code.Size = New System.Drawing.Size(518, 22)

            Me.lbl_exception_code.TabIndex = 4

            '

            'gbx_details

            '

            Me.gbx_details.Controls.Add(Me.rtb_inner_exception_desc)

            Me.gbx_details.Controls.Add(Me.btn_trace)

            Me.gbx_details.Controls.Add(Me.btn_next)

            Me.gbx_details.Controls.Add(Me.btn_prev)

            Me.gbx_details.Controls.Add(Me.lbl_inner_exception_code)

            Me.gbx_details.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

            Me.gbx_details.Location = New System.Drawing.Point(4, 114)

            Me.gbx_details.Name = "gbx_details"

            Me.gbx_details.Size = New System.Drawing.Size(524, 256)

            Me.gbx_details.TabIndex = 5

            Me.gbx_details.TabStop = False

            Me.gbx_details.Text = "详细(Details)"

            '

            'rtb_inner_exception_desc

            '

            Me.rtb_inner_exception_desc.BackColor = System.Drawing.SystemColors.InactiveBorder

            Me.rtb_inner_exception_desc.Location = New System.Drawing.Point(10, 44)

            Me.rtb_inner_exception_desc.Name = "rtb_inner_exception_desc"

            Me.rtb_inner_exception_desc.ReadOnly = True

            Me.rtb_inner_exception_desc.Size = New System.Drawing.Size(508, 206)

            Me.rtb_inner_exception_desc.TabIndex = 5

            Me.rtb_inner_exception_desc.Text = ""

            '

            'btn_trace

            '

            Me.btn_trace.Location = New System.Drawing.Point(436, 20)

            Me.btn_trace.Name = "btn_trace"

            Me.btn_trace.Size = New System.Drawing.Size(80, 22)

            Me.btn_trace.TabIndex = 4

            Me.btn_trace.Text = "跟踪(Trace)"

            '

            'btn_next

            '

            Me.btn_next.Location = New System.Drawing.Point(394, 20)

            Me.btn_next.Name = "btn_next"

            Me.btn_next.Size = New System.Drawing.Size(40, 22)

            Me.btn_next.TabIndex = 3

            Me.btn_next.Text = ">"

            '

            'btn_prev

            '

            Me.btn_prev.Location = New System.Drawing.Point(352, 20)

            Me.btn_prev.Name = "btn_prev"

            Me.btn_prev.Size = New System.Drawing.Size(40, 22)

            Me.btn_prev.TabIndex = 2

            Me.btn_prev.Text = "<"

            '

            'lbl_inner_exception_code

            '

            Me.lbl_inner_exception_code.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

            Me.lbl_inner_exception_code.Font = New System.Drawing.Font("Tahoma", 8.25!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))

            Me.lbl_inner_exception_code.ForeColor = System.Drawing.Color.Red

            Me.lbl_inner_exception_code.Location = New System.Drawing.Point(10, 20)

            Me.lbl_inner_exception_code.Name = "lbl_inner_exception_code"

            Me.lbl_inner_exception_code.Size = New System.Drawing.Size(340, 22)

            Me.lbl_inner_exception_code.TabIndex = 0

            '

            'rtb_exception_desc

            '

            Me.rtb_exception_desc.BackColor = System.Drawing.SystemColors.Control

            Me.rtb_exception_desc.Location = New System.Drawing.Point(6, 28)

            Me.rtb_exception_desc.Name = "rtb_exception_desc"

            Me.rtb_exception_desc.ReadOnly = True

            Me.rtb_exception_desc.Size = New System.Drawing.Size(518, 56)

            Me.rtb_exception_desc.TabIndex = 6

            Me.rtb_exception_desc.Text = ""

            '

            'Form_UException

            '

            Me.AcceptButton = Me.btn_ok

            Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)

            Me.ClientSize = New System.Drawing.Size(532, 375)

            Me.Controls.Add(Me.rtb_exception_desc)

            Me.Controls.Add(Me.gbx_details)

            Me.Controls.Add(Me.lbl_exception_code)

            Me.Controls.Add(Me.btn_details)

            Me.Controls.Add(Me.btn_ok)

            Me.Font = New System.Drawing.Font("Tahoma", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog

            Me.Name = "Form_UException"

            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

            Me.Text = "异常信息"

            Me.gbx_details.ResumeLayout(False)

            Me.ResumeLayout(False)

 

        End Sub

 

#End Region

 

        Private iboo_in_details As Boolean

        Private iexp_exception As UException

        Private iexp_current_inner_exception As System.Exception

 

        Private Sub btn_details_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_details.Click

            If iboo_in_details = False Then

                iboo_in_details = True

            Else

                iboo_in_details = False

            End If

            ResizeWithInDetails()

        End Sub

 

        Private Sub Form_UException_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

            iboo_in_details = False

            ResizeWithInDetails()

        End Sub

 

        Private Sub ResizeWithInDetails()

            If iboo_in_details = False Then

                Me.Size = New System.Drawing.Size(540, 145)

            Else

                Me.Size = New System.Drawing.Size(540, 400)

            End If

        End Sub

 

        Friend Sub SetException(ByVal aexp_exception As UException)

            iexp_exception = aexp_exception

            Me.lbl_exception_code.Text = iexp_exception.GetErrorCode()

            Me.rtb_exception_desc.Text = iexp_exception.GetErrorDescription()

            If Not iexp_exception.InnerException Is Nothing Then

                Me.btn_details.Enabled = True

                Me.iexp_current_inner_exception = iexp_exception.InnerException

 

                ShowInnerException()

            Else

                Me.btn_details.Enabled = False

            End If

        End Sub

 

        Private Sub ShowInnerException()

            If TypeOf iexp_current_inner_exception Is Exceptions.UException Then

                Me.lbl_inner_exception_code.Text = CType(iexp_current_inner_exception, Exceptions.UException).GetErrorCode

                Me.rtb_inner_exception_desc.Text = CType(iexp_current_inner_exception, Exceptions.UException).GetErrorDescription

            Else

                Me.rtb_inner_exception_desc.Text = iexp_current_inner_exception.Message

            End If

            If iexp_current_inner_exception.InnerException Is Nothing Then

                btn_next.Enabled = False

            Else

                btn_next.Enabled = True

            End If

            If iexp_current_inner_exception.GetBaseException Is Nothing Then

                btn_prev.Enabled = False

            Else

                btn_prev.Enabled = True

            End If

        End Sub

 

        Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click

            Me.Close()

        End Sub

 

        Private Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_next.Click

            iexp_current_inner_exception = iexp_current_inner_exception.InnerException

            ShowInnerException()

            'If Not iexp_current_inner_exception.InnerException Is Nothing Then

            '    btn_next.Enabled = True

            'Else

            '    btn_prev.Enabled = True

            '    btn_next.Enabled = False

            'End If

        End Sub

 

 

        Private Sub btn_prev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_prev.Click

            iexp_current_inner_exception = iexp_current_inner_exception.GetBaseException

            ShowInnerException()

            'If Not iexp_current_inner_exception.GetBaseException Is Nothing Then

            '    btn_prev.Enabled = True

            'Else

            '    btn_prev.Enabled = False

            '    btn_next.Enabled = True

            'End If

        End Sub

 

        Private Sub btn_trace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_trace.Click

            System.Windows.Forms.MessageBox.Show(iexp_current_inner_exception.ToString())

        End Sub

    End Class

End Namespace

 

使用Exception类自定义确误代码即其相关信息

 

Imports ASAT.Exceptions

 

Namespace Sublot.Exceptions

    Public Class Exception_Sublot

        Inherits UException

 

        Public Overrides Sub Initialize()

            ierm_error_msg_list = New UErrorMessageCollection

            ierm_error_msg_list.Add(New ASAT.Exceptions.UErrorMessage("TEST-1001", "TEST[%1]!", "Can Not Find Correspoding Information About This Menu TEST [%1].!"))

        End Sub

 

        Public Sub New(ByVal astg_error_code As String)

            MyBase.New(astg_error_code)

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal aexp_inner_exception As System.Exception)

            MyBase.New(astg_error_code, aexp_inner_exception)

        End Sub

 

        Public Sub New(ByVal astg_error_code As String, ByVal astg_error_customer_error_msg_1 As String)

            MyBase.New(astg_error_code, astg_error_customer_error_msg_1)

        End Sub

 

    End Class

End Namespace

 

异常类的使用

 

直接在Form 中使用

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Try

            Throw New ASAT.Sublot.Exceptions.Exception_Sublot("TEST-1001", "xxxxxxxxxxx")

        Catch ex As Exceptions.UException

            ex.ShowErrorDescription()

        End Try

 

End Sub

 

在类中使用抛出,在Form 中来接

Class_sublot.vb

 

        Public Function getSublotIDFromNo() As String

 

            Try

                Dim i, j, k As Integer

                i = 1

                j = 0

                k = i / j

            Catch ex As OverflowException

                Throw New ASAT.Sublot.Exceptions.Exception_Sublot("TEST-1001", ex)

            End Try

 

        End Function

        Public Sub xx()

            Try

                getSublotIDFromNo()

            Catch ex As Exceptions.UException

                ex.ShowErrorDescription()

            End Try

 

        End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'iobj_print.ShellTest()

 

        Try

 

            iobj_sublot.xx()

 

        Catch ex As Exceptions.UException

            ex.ShowErrorDescription()

        End Try

 

End Sub

 

--效果图

posted on 2009-04-01 10:09  封起De日子  阅读(200)  评论(0)    收藏  举报

导航