自定义异常类的使用
-首先定义三个基本类: 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