×

VB.Net Programs

VB.Net program to demonstrate the example of ArgumentException

By Nidhi Last Updated : November 11, 2024

ArgumentException in VB.Net

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 code to demonstrate the example of ArgumentException

'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 »


Comments and Discussions!

Load comments ↻





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