ADVERTISEMENT
ADVERTISEMENT

VB.Net program to override the Message property of DivideByZeroException class

Here, we are going to learn how to override the Message property of DivideByZeroException class in VB.Net?
Submitted by Nidhi, on January 05, 2021

Here, we will create our own exception class by inheriting the DivideByZeroException class and override the Message property.

Program/Source Code:

The source code to override the Message property of DivideByZeroException class is given below. The given program is compiled and executed successfully.

'Vb.Net program to override the Message property of 
'DivideByZeroException class.

Imports System.Threading

Module Module1
    Class MyException
        Inherits DivideByZeroException

        Public Overrides ReadOnly Property Message As String
            Get
                Return MyBase.Message
            End Get
        End Property
    End Class

    Sub Main()
        Try
            Dim num1 As Integer = 15
            Dim num2 As Integer = 0
            Dim num3 As Integer = 0

            If num2 = 0 Then
                Throw New MyException()
            Else
                num3 = num1 \ num2
                Console.WriteLine("Result : {0}", num3)
            End If
        Catch e As MyException
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Module

Output:

Attempted to divide by zero.
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 override the Message property in the MyException class.

The Main() is the entry point for the program, here we created three variable num1, num2, and num3 that are initialized with 10,0,0. Here, we checked the value of num2 if it is 0 then thrown the object of MyException class, which is caught in the catch block, and print the appropriate message on the console screen.

VB.Net Exception Handling Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

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.