Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the multiple catch blocks
By Nidhi Last Updated : November 13, 2024
Multiple catch blocks in VB.Net
Here, we will demonstrate the multiple catch blocks, as we know that the program may generate different kinds of exceptions according to the input values of variables, and then we handle the exceptions using the multiple catch blocks.
Program/Source Code:
The source code to demonstrate the multiple catch blocks is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of multiple catch blocks
'Vb.Net program to demonstrate the multiple catch blocks.
Module Module1
Sub Main()
Dim num1 As Integer = 0
Dim num2 As Integer = 0
Dim num3 As Integer = 0
Try
Console.Write("Enter the value of num1: ")
num1 = Integer.Parse(Console.ReadLine())
Console.Write("Enter the value of num2: ")
num2 = Integer.Parse(Console.ReadLine())
num3 = num1 \ num2
Console.WriteLine("Num3 : " + num3)
Catch e As DivideByZeroException
Console.WriteLine(e.Message)
Catch e As FormatException
Console.WriteLine("Format exception")
Catch e As Exception
Console.WriteLine("Other exception")
End Try
End Sub
End Module
Output
Enter the value of num1: 12
Enter the value of num2: 0
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 that are initialized with 0.
Here, we defined the Try block to handle exceptions. Then we read the value from the user. According to the input, the program may generate a different-different exception and then an appropriate message will be printed on the console screen.
VB.Net Exception Handling Programs »