ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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
            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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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

© https://www.includehelp.com some rights reserved.