VB.Net program to demonstrate the NotSupportedException

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

NotSupportedException in VB.Net

Here, we will demonstrate the NotSupportedException, in this program, the stream does not support write operation.

Program/Source Code:

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

VB.Net code to demonstrate the example of NotSupportedException

'Vb.Net program demonstrates the NotSupportedException.

Imports System.IO
Imports System.Text
Imports System.Threading.Tasks

Module Module1
    Sub Main()
        Try
            Dim enc As Encoding = Encoding.Unicode
            Dim value As String = "This is a string to persist."
            Dim bytes() As Byte = enc.GetBytes(value)

            Dim fs As New FileStream("sample.txt",
                                     FileMode.Open,
                                     FileAccess.Read)

            Dim task1 As Task = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)

            Dim task2 As Task = task1.ContinueWith(Function(a) fs.WriteAsync(bytes, 0, bytes.Length))

            fs.Close()
        Catch ex As NotSupportedException
            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 used multithreading to perform the write operation in the file but the above code will generate NotSupprotedException that will be caught in the catch block and print an appropriate message on the console screen.

VB.Net Exception Handling Programs »





Comments and Discussions!

Load comments ↻





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