VB.Net program to demonstrate the InvalidOperationException

Here, we are going to demonstrate the InvalidOperationException in VB.Net.
Submitted by Nidhi, on January 07, 2021

Here, we will demonstrate the InvalidOperationException and print an appropriate message on the console screen.

Program/Source Code:

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

'Vb.Net program demonstrate the InvalidOperationException.

Imports System.IO

Module Module1
    Sub Main()
            Dim nums As New List(Of Integer)()


            For Each num In nums
                Dim square As Integer

                square = Math.Pow(num, 2)
                Console.WriteLine("Adding {0} to the List", square)

        Catch ex As InvalidOperationException
            Console.WriteLine("Exception: " & ex.Message)
        End Try
    End Sub
End Module


Adding 1 to the List
Exception: Collection was modified; enumeration operation may not execute.
Press any key to continue . . .


In the above program, we created a module Module1 that contains a Main() function.

The Main() function is the entry point for the program. Here, we created a list collection of integer numbers and then calculated the square of a number and add the square of the number into the list then Invalid operation exception gets generated and caught in the catch block and print the appropriate message 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.