Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the IOException
By Nidhi Last Updated : November 11, 2024
IOException in VB.Net
Here, we will demonstrate the IOException, here we open a serial port that does not exist in the computer system then IOException gets generated.
Program/Source Code:
The source code to demonstrate the input/output exception (IOException) is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of IOException
'Vb.Net program to demonstrate the IOException.
Imports System.IO
Imports System.IO.Ports
Module Module1
Sub Main()
Try
Dim data As String
Dim serial As New SerialPort("COM1", 4800, Parity.Odd, 8, StopBits.One)
serial.Open()
data = serial.ReadLine()
serial.Close()
Console.WriteLine(data)
Catch ex As IOException
Console.WriteLine("Exception: " & ex.Message)
End Try
End Sub
End Module
Output
Exception: The port 'COM1' does not exist.
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 the object of SerialPort class and then read data from the serial port, but in our case, the "COM1" port does not exist in the computer. Then IOException gets generated that will be caught in the catch block and print the appropriate message on the console screen.
VB.Net Exception Handling Programs »