×

VB.Net Programs

VB.Net program to demonstrate the DivideByZeroException

By Nidhi Last Updated : November 11, 2024

VB.Net - DivideByZeroException Class (System)

Here, we demonstrate the divide by zero exception. As we know that the divide zero operation is not valid, that’s why to handle the exception, we need to use DivideByZeroException class in the catch block.

Program/Source Code:

The source code to demonstrate the DivideByZeroException is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of DivideByZeroException

'Vb.Net program to demonstrate the DivideByZeroException.

Module Module1
    Sub Main()
        Dim num1 As Integer = 10
        Dim num2 As Integer = 0
        Dim num3 As Integer = 0

        Try
            num3 = num1 \ num2
            Console.WriteLine("Result is: {0}", num3)
        Catch e As DivideByZeroException
            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. The Module1 contains Main() function.

The Main() is the entry point for the program, here we created three local variables num1, num2, and num3. Which are initialized with 10, 0, 0 respectively.

Here we defined the Try block to handle exceptions. The above will generate DivideByZeroException because we are trying to divide an integer value by zero in the below statement.

num3 = num1 \ num2

VB.Net Exception Handling Programs »


Comments and Discussions!

Load comments ↻





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