VB.Net用户定义的异常示例? [英] VB.Net Examples of User-Defined Exceptions?

查看:148
本文介绍了VB.Net用户定义的异常示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单地说,我想知道这里是否有人可以给我一个VB.Net中用户定义的异常示例。我已经有两个可以在网上找到的示例,但是除此之外,我再也想不到。我需要至少找到5个要放入笔记中的内容,然后提交给我的老师。

Very simply put, I am wondering if anybody here can give me an example of a user-defined exception in VB.Net. I already have two examples that I was able to find online, but other than that, I cannot think of any more. I need to find at least 5 to put in my notes, and then submit to my teacher.

到目前为止,我有两个是:无效的登录信息(例如不正确的登录信息)用户名或密码),以及在线商店上过期的信用卡信息。任何帮助将不胜感激。

The two I have so far are: invalid login information (such as improper username or password), and expired credit card information on an online store. Any help would be greatly appreciated.

推荐答案

基本要求是向您的项目中添加一个新类,该类继承自已构建的类。 -in类 System.Exception 。这几乎为您提供了免费所需的所有东西,因为它们全部在 System.Exception 类内部实现。

The basic requirement is that you add a new class to your project that inherits from the built-in class System.Exception. That gets you almost everything you need for free, because it's all implemented inside of the System.Exception class.

您唯一需要添加到类文件中的就是构造函数(因为请记住,构造函数不会被继承)。尽管您不必定义所有三个标准构造函数,但我强烈建议您这样做,只是为了使您的界面与.NET Framework提供的所有异常类保持一致。一次定义它们并不难,并且代码分析工具推荐

The only thing you need to add to the class file are the constructors (because, remember, constructors are not inherited). While you don't have to define all three standard constructors, I highly recommend that you do so, just so that your interface is consistent with all of the exception classes provided by the .NET Framework. It's not that hard to just define them once, and recommended by code analysis tools.

最后(这是大多数人(包括发布了该问题其他答案的人们所忽略的步骤),您需要做出例外可序列化。通过添加 SerializableAttribute 添加到类声明中,并添加一个受保护的构造函数,该构造函数在内部由序列化机制使用。该属性是 not 继承自 System.Exception 的属性,必须明确声明。

And finally (this is the step forgotten by most people, including by those people who posted the other answers to this question), you need to make your exception serializable. Do that by adding the SerializableAttribute to the class declaration and by adding a Protected constructor that is used internally by the serialization mechanism. The attribute is not inherited from System.Exception, and must be stated explicitly.

由于您的要求很好,因此以下是一个完整的示例,其中实现了上述所有功能:

Since you asked so nicely, here's a complete example, with all of the above implemented:

''' <summary>
''' The exception that is thrown when DWM composition fails or is not
''' supported on a particular platform.
''' </summary>
<Serializable()> _
Public Class DwmException : Inherits System.Exception

    ''' <summary>
    ''' Initializes a new instance of the <see cref="DwmException"/> class.
    ''' </summary>
    Public Sub New()
        MyBase.New()
    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="DwmException"/> class
    ''' with the specified error message.
    ''' </summary>
    ''' <param name="message">The message that describes the error.</param>
    Public Sub New(ByVal message As String)
        MyBase.New(message)
    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="DwmException"/> class
    ''' with the specified error message and a reference to the inner
    ''' exception that is the cause of this exception.
    ''' </summary>
    ''' <param name="message">The message that describes the error.</param>
    ''' <param name="innerException">The exception that is the cause of the
    ''' current exception, or a null reference if no inner exception is
    ''' specified</param>
    Public Sub New(ByVal message As String, ByVal innerException As System.Exception)
        MyBase.New(message, innerException)
    End Sub

    ' Constructor required for serialization
    <SecuritySafeCritical()> _
    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub

End Class






Ahh,发布以上内容后,我意识到我可能已经完全误解了您的问题。我(和其他答案)都以为您在问一个示例,该示例如何实现用户定义的异常类。看起来您只是在问示例,当您在您自己的项目中做这样的事情时……这要棘手得多。


Ahh, after posting the above, I realized that I might have completely misinterpreted your question. I (and the other answers) thought you were asking for an example of how to implement a user-defined exception class. It looks like you're just asking of examples when you would do such a thing in your own project... That's much trickier.

大部分时间,您不想这样做。唯一应该抛出自定义异常的时间是在编写可重用的代码库(例如 .DLL 文件)时,并且您希望客户端代码对根据所引发的特定异常而不同。因此,就我而言,我从类库中抛出 DwmException ,因为在未启用DWM合成的情况下,客户端应用程序可能想捕获该异常并禁用一些精美的UI功能用户的计算机。通常,我只是抛出一个标准的 NotSupportedException ,但是在这种情况下,我想为客户端提供区分他们可以处理的异常和他们不能或不应该处理的异常的能力。

Most of the time, you don't want to do this. The only time you should throw a custom exception is when you're writing a reusable code library (like a .DLL file), and you expect the client code to react differently based on the particular exception that was thrown. So, in my case, I throw a DwmException from my class library because the client application might want to catch that exception and disable some fancy UI feature when DWM composition is not enabled on the user's computer. Ordinarily, I would just throw a standard NotSupportedException, but in this case, I want to provide the client with the ability to distinguish between an exception they can handle and one that they can't or shouldn't.

在标准应用程序中,所有代码都是独立的(即,当您创建可重用的代码库时) ,基本上应该从不创建/抛出自定义异常。投掷标准之一。遵循上述规则,如果需要引发自定义异常以影响客户端代码的反应方式,则表明您在应用程序内部使用异常进行流控制(因为生产者和使用者是一个同样),强烈建议不要这样做。

In a standard application, where all the code is self-contained (i.e., when you are not creating a reusable library of code), you should basically never create/throw a custom exception. Throw one of the standard ones, instead. Following the above rule, if you needed to throw a custom exception to affect how the client code reacted, that would be an indication that you're using exceptions for "flow control" inside of your app (since the producer and the consumer are one in the same), which is strongly discouraged.

这篇关于VB.Net用户定义的异常示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆