ADVERTISEMENT
ADVERTISEMENT

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

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



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.