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

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






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.