VB.Net program to demonstrate the example of ArgumentNullException

Here, we are going to demonstrate the example of ArgumentNullException in VB.Net.
Submitted by Nidhi, on January 07, 2021 [Last updated : March 08, 2023]

ArgumentNullException in VB.Net

Here, we will create a function that accepts the filename as an argument, but we will pass Nothing in-place of argument then ArgumentNullException will be generated.

Program/Source Code:

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

VB.Net code to demonstrate the example of ArgumentNullException

'Vb.Net program demonstrate the Argument Null Exception.

Imports System.IO

Module Module1
    Class Sample
        Public Sub PrintData(ByVal fileName As String)
            Dim luckyNum As Integer = 0
            Dim msg As String = ""

            Dim f As FileStream = File.Open(fileName, FileMode.Open)
            Dim breader As New BinaryReader(f)

            luckyNum = breader.ReadInt32()
            msg = breader.ReadString()

            Console.WriteLine("Lucky Number : " & luckyNum)
            Console.WriteLine("String       : " & msg)

            f.Close()
        End Sub
    End Class

    Sub Main()
        Dim S As New Sample()

        Try
            S.PrintData(Nothing)
        Catch ex As ArgumentNullException
            Console.WriteLine("Argument Null Exception occurred")
        End Try
    End Sub
End Module

Output

Argument Null Exception occurred
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 method PrintData(), In the PrintData() method, we passed filename as an argument and read data and print data on the console screen.

The Main() function is the entry point for the program. Here, we passed 'Nothing" in place of argument then ArgumentNullException will be generated and then we printed the appropriate message on the console screen.

VB.Net Exception Handling Programs »





Comments and Discussions!

Load comments ↻





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