VB.Net Examples of custom exceptions?

It is very simple to put, I am wondering if anyone here can give me an example of a custom exception in VB.Net. I already have two examples that I could find on the Internet, but apart from that I can no longer think. I need to find at least 5 to record my notes and then obey my teacher.

The two that I still have are: incorrect login information (for example, incorrect username or password) and expired credit card information in the online store. Any help would be greatly appreciated.

+3
source share
2 answers

The basic requirement is that you add a new class to your project, which inherits from the built-in class System.Exception. This gives you almost everything you need for free, because it is implemented inside the class System.Exception.

The only thing you need to add to the class file is the constructors (because, remember, constructors are not inherited). Although you do not need to define all three standard constructors, I strongly recommend that you do this just to make your interface match all the exception classes provided by the .NET Framework. It is not so difficult to define them once, but it is recommended using code analysis tools .

( , , , ), . , SerializableAttribute Protected, . System.Exception .

, , :

''' <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, , , . ( ) , , . , , ​​ ... .

. , , - (, .DLL), , - , . , DwmException , , DWM . NotSupportedException, , .

, (.. ), / . . , , , , " " ( ), .

+20

All Articles