VB.Net program to demonstrate the multiple catch blocks

Here, we are going to demonstrate the multiple catch blocks in VB.Net.
Submitted by Nidhi, on January 05, 2021

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.