VB.Net program to create our own exception class

Here, we are going to learn how to create our own exception class in VB.Net?
Submitted by Nidhi, on January 05, 2021 [Last updated : March 08, 2023]

Creating own exception class in VB.Net

Here, we will create our own exception class by inheriting the Exception class.

Program/Source Code:

The source code to create your own exception class is given below. The given program is compiled and executed successfully.

VB.Net code to create our own exception class

'Vb.Net program to create our own exception class.

Imports System.Threading

Module Module1
    Class MyException
        Inherits Exception
        Private Info As String
        Public Property ExpInfo() As String
            Get
                Return Info
            End Get
            Set(ByVal value As String)
                Info = value
            End Set
        End Property
    End Class

    Sub Main()
        Try
            Dim m As MyException
            m = New MyException()
            m.ExpInfo = "My Exception Message"
            Throw m
        Catch e As MyException
            Console.WriteLine(e.ExpInfo)
        End Try
    End Sub
End Module

Output

My Exception Message
Press any key to continue . . .

Explanation

In the above program, here we created a module Module1. In Module1 we created the class MyException, here we inherit the Exception class and implement a property to set and get the error message.

The Main() is the entry point for the program, here we created the object of MyException class and set the error message, and throw the exception using the Throw keyword. Then the given message will be printed on the console screen.

VB.Net Exception Handling Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.