TimeoutException 类

定义

当为进程或操作分配的时间已过期时引发的异常。

public ref class TimeoutException : Exception
public ref class TimeoutException : SystemException
public class TimeoutException : Exception
public class TimeoutException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class TimeoutException : SystemException
type TimeoutException = class
    inherit Exception
type TimeoutException = class
    inherit SystemException
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type TimeoutException = class
    inherit SystemException
Public Class TimeoutException
Inherits Exception
Public Class TimeoutException
Inherits SystemException
继承
TimeoutException
继承
TimeoutException
派生
属性

示例

下面的代码示例演示如何 TimeoutException 将 与 类的成员 System.IO.Ports.SerialPort 结合使用。

// This example demonstrates the use of the TimeoutException
// exception in conjunction with the SerialPort class.

#using <System.dll>

using namespace System;
using namespace System::IO::Ports;

int main()
{
    String^ input;
    try
    {
        // Set the COM1 serial port to speed = 4800 baud, parity = odd,
        // data bits = 8, stop bits = 1.
        SerialPort^ port = gcnew SerialPort("COM1",
            4800, Parity::Odd, 8, StopBits::One);
        // Timeout after 2 seconds.
        port->ReadTimeout = 2000;
        port->Open();

        // Read until either the default newline termination string
        // is detected or the read operation times out.
        input = port->ReadLine();

        port->Close();

        // Echo the input.
        Console::WriteLine(input);
    }

    // Only catch timeout exceptions.
    catch (TimeoutException^ ex)
    {
        Console::WriteLine(ex);
    }
};
/*
This example produces the following results:

(Data received at the serial port is echoed to the console if the
read operation completes successfully before the specified timeout period
expires. Otherwise, a timeout exception like the following is thrown.)

System.TimeoutException: The operation has timed-out.
at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
at System.IO.Ports.SerialPort.ReadTo(String value)
at System.IO.Ports.SerialPort.ReadLine()
at Sample.Main()
*/
// This example demonstrates the use of the TimeoutException
// exception in conjunction with the SerialPort class.

using System;
using System.IO.Ports;

class Sample
{
    public static void Main()
    {
    string input;
    try
        {
// Set the COM1 serial port to speed = 4800 baud, parity = odd,
// data bits = 8, stop bits = 1.
        SerialPort sp = new SerialPort("COM1",
                        4800, Parity.Odd, 8, StopBits.One);
// Timeout after 2 seconds.
        sp.ReadTimeout = 2000;
        sp.Open();

// Read until either the default newline termination string
// is detected or the read operation times out.
        input = sp.ReadLine();

        sp.Close();

// Echo the input.
        Console.WriteLine(input);
        }

// Only catch timeout exceptions.
    catch (TimeoutException e)
        {
        Console.WriteLine(e);
        }
    }
}
/*
This example produces the following results:

(Data received at the serial port is echoed to the console if the
read operation completes successfully before the specified timeout period
expires. Otherwise, a timeout exception like the following is thrown.)

System.TimeoutException: The operation has timed-out.
   at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadTo(String value)
   at System.IO.Ports.SerialPort.ReadLine()
   at Sample.Main()
*/
// This example demonstrates the use of the TimeoutException
// exception in conjunction with the SerialPort class.

open System
open System.IO.Ports

try
// Set the COM1 serial port to speed = 4800 baud, parity = odd,
// data bits = 8, stop bits = 1.
    let sp = new SerialPort("COM1", 4800, Parity.Odd, 8, StopBits.One)
// Timeout after 2 seconds.
    sp.ReadTimeout <- 2000
    sp.Open()

// Read until either the default newline termination string
// is detected or the read operation times out.
    let input = sp.ReadLine()

    sp.Close()

// Echo the input.
    printfn $"{input}"

// Only catch timeout exceptions.
with :? TimeoutException as e ->
    printfn $"{e}"
(*
This example produces the following results:

(Data received at the serial port is echoed to the console if the
read operation completes successfully before the specified timeout period
expires. Otherwise, a timeout exception like the following is thrown.)

System.TimeoutException: The operation has timed-out.
   at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
   at System.IO.Ports.SerialPort.ReadTo(String value)
   at System.IO.Ports.SerialPort.ReadLine()
   at Sample.main()
*)
' This example demonstrates the use of the TimeoutException 
' exception in conjunction with the SerialPort class.

Imports System.IO.Ports

Class Sample
   Public Shared Sub Main()
      Dim input As String
      Try
         ' Set the COM1 serial port to speed = 4800 baud, parity = odd, 
         ' data bits = 8, stop bits = 1.
         Dim sp As New SerialPort("COM1", 4800, Parity.Odd, 8, StopBits.One)

         ' Timeout after 2 seconds.
         sp.ReadTimeout = 2000
         sp.Open()
         
         ' Read until either the default newline termination string 
         ' is detected or the read operation times out.
         input = sp.ReadLine()

         sp.Close()

         ' Echo the input.
         Console.WriteLine(input)
      
      ' Only catch timeout exceptions.
      Catch e As TimeoutException
         Console.WriteLine(e)
      End Try
   End Sub
End Class
'
'This example produces the following results:
'
'(Data received at the serial port is echoed to the console if the 
'read operation completes successfully before the specified timeout period 
'expires. Otherwise, a timeout exception like the following is thrown.)
'
'System.TimeoutException: The operation has timed-out.
'   at System.IO.Ports.SerialStream.ReadByte(Int32 timeout)
'   at System.IO.Ports.SerialPort.ReadOneChar(Int32 timeout)
'   at System.IO.Ports.SerialPort.ReadTo(String value)
'   at System.IO.Ports.SerialPort.ReadLine()
'   at Sample.Main()
'

注解

有关在引发异常之前 TimeoutException 设置超时间隔的信息,请参阅文档,了解方法超时的类型。有关处理超时的信息,请参阅引发异常的方法的文档。

TimeoutException 使用 HRESULT COR_E_TIMEOUT,其值为 0x80131505。

有关实例的初始属性值的列表TimeoutException,请参阅TimeoutException构造函数。

构造函数

TimeoutException()

初始化 TimeoutException 类的新实例。

TimeoutException(SerializationInfo, StreamingContext)

用序列化数据初始化 TimeoutException 类的新实例。

TimeoutException(String)

使用指定的错误消息初始化 TimeoutException 类的新实例。

TimeoutException(String, Exception)

使用指定的错误消息和内部异常初始化 TimeoutException 类的新实例。

属性

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。

(继承自 Exception)
Message

获取描述当前异常的消息。

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。

(继承自 Exception)
TargetSite

获取引发当前异常的方法。

(继承自 Exception)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。

(继承自 Exception)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfo

(继承自 Exception)
GetType()

获取当前实例的运行时类型。

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。

(继承自 Exception)

事件

SerializeObjectState
已过时.

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。

(继承自 Exception)

适用于

另请参阅