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

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 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
                Return Info
            End Get
            Set(ByVal value As String)
                Info = value
            End Set
        End Property
    End Class

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


My Exception Message
Press any key to continue . . .


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



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.