自定义异常类的使用
-首先定义三个基本类: 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 color: b
分享到:
相关推荐
### Java自定义异常类详解 #### 一、Java异常体系概览 在Java语言中,异常处理机制是一种用于处理程序运行时错误的重要机制。Java中的异常处理基于`java.lang.Throwable`类,它有两个重要的子类:`Exception`和`...
通过创建自定义异常类,开发者可以更精确地捕捉并处理应用程序中的特定错误场景,提高代码的可读性和维护性。 #### 三、自定义异常的实现步骤 ##### 1. 创建自定义异常类 自定义异常类通常继承自`Exception`或其...
1. **创建自定义异常类**:自定义异常需要创建一个新的类,这个类通常是`Exception`类或者其子类的实例。例如: ```python class CustomException(Exception): def __init__(self, message): self.message = ...
本文将详细介绍如何在 Java 中创建自定义异常类,并通过一个示例来展示如何使用这些自定义异常。 #### 创建自定义异常类 自定义异常类是指开发者根据实际需求自定义的异常类型。在 Java 中,创建自定义异常类通常...
在这个例子中,`CustomException`是我们创建的自定义异常类,它包含了`message`属性来存储错误信息。 2. 在适当的地方抛出(raise)自定义异常。例如: ```python try: # 检查某种条件 if condition: raise ...
Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE程序 自定义异常类Java SE...
1. **自定义异常类** 在 Java 中,自定义异常通常继承自 `Exception` 或其子类。例如,你可以创建一个名为 `CustomServiceException` 的异常类,它可能包含特定的错误码和错误信息: ```java public class ...
为了自定义异常处理逻辑,我们可以创建一个实现`ResponseErrorHandler`接口的类。这样,当`RestTemplate`遇到错误响应时,会调用我们自定义的异常处理方法,而不是直接抛出默认异常。 例如,我们可以创建一个名为`...
### Java自定义异常类知识点详解 #### 一、概述 在Java编程中,自定义异常是一种常见的编程实践,它能够帮助开发者更精确地控制程序的行为并处理错误情况。通过创建自定义异常类,我们可以根据应用的具体需求来抛...
### JAVA自定义异常类 ...以上示例展示了如何创建和使用自定义异常类,以及如何根据输入数据进行条件判断。自定义异常类可以更加精确地捕捉和处理特定的错误情况,有助于提高程序的健壮性和可读性。
本文将基于给定的文件信息,深入解析“自定义异常类步骤”这一主题,涵盖异常处理的基本概念、自定义异常类的设计与实现,以及如何在实际代码中应用这些自定义异常。 ### 异常处理概述 异常处理是一种编程机制,...
我们可以创建一个名为`NegativeDepositException`的自定义异常类,如下所示: ```java public class NegativeDepositException extends Exception { public NegativeDepositException(String message) { super...
在这个例子中,`CustomException`是一个自定义异常类,它扩展了`Exception`类。`CustomException`提供了两个构造器:一个无参构造器,调用父类的无参构造器;另一个接受字符串参数的构造器,用于传递错误信息。此外...
总结而言,通过实现`IntegerException`自定义异常类,并在`People`类的`setAge`方法中使用,我们有效地控制了年龄值的合法性,增强了程序的鲁棒性和用户友好性。这种异常处理策略是编写高质量、健壯的Java应用程序的...
本资源“python烟花代码-24-自定义异常代码实现之异常类.ev4.rar”显然关注的是如何在Python中自定义异常类,这在处理特定错误或提供更详细的错误信息时非常有用。下面我们将深入探讨Python自定义异常的相关知识点。...
通过创建自定义异常类,抛出和捕获异常,我们可以确保程序在遇到预期错误时能够给出明确的反馈,并采取适当的措施。同时,结合合适的工具和组件,可以进一步优化异常处理机制,提升软件的用户体验。
以下是一个简单的自定义异常类的示例: ```csharp public class CustomException : Exception { public CustomException() : base() { } public CustomException(string message) : base(message) { } ...
### Java软件开发实战:自定义异常类 #### 1. 什么是自定义异常类? 在Java编程中,虽然已经...通过以上内容的学习,开发者可以更好地理解和掌握自定义异常类的定义和使用方法,从而提高Java程序的健壮性和可维护性。
创建一个自定义异常类通常包括以下几个步骤: 1. 创建新的异常类:扩展`Exception`或其子类,如`IllegalArgumentException`,并提供必要的构造函数。例如,我们可以创建一个`InvalidCredentialsException`类: ```...
#### (2)自定义异常类的定义及使用 - **继承异常类型**:创建自定义异常类时,通常需要继承`Exception`或`RuntimeException`。如果希望异常为编译时异常,继承`Exception`;如果希望异常为运行时异常,继承`...