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 [Last updated : March 08, 2023]

Overriding the Message property of DivideByZeroException class in VB.Net

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 code to override the Message property of DivideByZeroException class

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





Comments and Discussions!

Load comments ↻





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