VB.Net program to demonstrate the ObjectDisposedException

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

ObjectDisposedException in VB.Net

Here, we will demonstrate the ObjectDisposedException, when we use the disposed of an object to access the member of the class then ObjectDisposedException gets generated.

Program/Source Code:

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

VB.Net code to demonstrate the example of ObjectDisposedException

'Vb.Net program demonstrate the 
'ObjectDisposedException.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim memstream As New MemoryStream(10)
            memstream.Close()

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

Output

Exception: Stream does not support writing.
Press any key to continue . . .

Explanation

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

The Main() function is the entry point for the program. Here, we created an object of MemoryStream class and then close the stream using the Close() method. After that we called the ReadByte() method then the program will generate ObjectDisposedException that will be caught in the catch block and print 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.