×

VB.Net Programs

VB.Net program to demonstrate the TimeoutException

By Nidhi Last Updated : November 14, 2024

TimeoutException in VB.Net

Here, we will set timeout to read data from serial port, if we will not any response from serial port, then TimeoutException will be generated.

Program/Source Code:

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

VB.Net code to demonstrate the example of TimeoutException

'Vb.Net program to demonstrate the TimeoutException.

Imports System.IO
Imports System.IO.Ports

Module Module1
    Sub Main()
        Try
            Dim data As String
            Dim serial As New SerialPort("COM3", 4800, Parity.Odd, 8, StopBits.One)

            serial.ReadTimeout = 3000
            serial.Open()
            data = serial.ReadLine()
            serial.Close()
            Console.WriteLine(data)

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

Output

Exception: The operation has timed-out.
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 open the serial port and set the timeout to read data. When the timeout gets finished without reading data from the serial port then a TimeoutException gets generated 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.