ADVERTISEMENT
ADVERTISEMENT

VB.Net program to demonstrate the example of ArgumentException

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

Here, we will throw an ArgumentException when the argument of the DivideNumberByTwo() method is not an even number.

Program/Source Code:

The source code to demonstrate the example of ArgumentException is given below. The given program is compiled and executed successfully.

'Vb.Net program to demonstrate the ArgumentException.

Imports System.IO

Module Module1
    Class Sample
        Shared Function DivideNumberByTwo(ByVal num As Integer) As Integer
            If (num Mod 2 <> 0) Then
                Throw New ArgumentException(String.Format("{0} is not an even number", num), "num")
            End If
            Return num / 2
        End Function

    End Class

    Sub Main()
        Try
            Sample.DivideNumberByTwo(5)
        Catch ex As ArgumentException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Module

Output:

5 is not an even number
Parameter name: num
Press any key to continue . . .

Explanation:

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

The Sample class contains a Shared method DivideNumberByTwo(), In the DivideNumberByTwo() method, we checked the value of the argument is an even number or not. If the argument contains an odd value then we throw argument exception explicitly.

VB.Net Exception Handling Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.