我正在为设备 IO 类定义一个异常类。
当与设备的通信超时时,我想抛出一个异常。以下是我目前拥有的:
/// <summary>
/// The exception that is thrown when the time allotted for talking to the FANUC controller has expired.
/// </summary>
[Serializable]
public class FanucTimeoutException : TimeoutException
{
/// <summary>
/// Initializes a new instance of the <c>FanucLib.FanucTimeoutException</c> class.
/// </summary>
public FanucTimeoutException() { }
/// <summary>
/// Initializes a new instance of the <c>FanucLib.FanucTimeoutException</c> class with the specified error message.
/// </summary>
/// <param name="message">The message that describes the error. </param>
public FanucTimeoutException(string message) : base(message)
{
}
/// <summary>
/// Initializes a new instance of the <c>FanucLib.FanucTimeoutException</c> class with the specified error message and the address trying to access.
/// </summary>
/// <param name="message">The message that describes the error. </param>
/// <param name="address">The address trying to access.</param>
public FanucTimeoutException(string message, string address) : base($"{message} Address: {address}.")
{
}
public FanucTimeoutException(string message, Exception innerException) : base(message, innerException)
{
}
public FanucTimeoutException(string message, string address, Exception innerException) : base($"{message} Address: {address}.", innerException)
{
}
/// <inheritdoc />
public FanucTimeoutException(SerializationInfo info, StreamingContext context)
{
}
}
但是,我不确定 throw 是否是一个好习惯TimeoutException。无论C#教程和净指导对例外的设计不谈TimeoutException。他们只建议使用少数异常类型,例如InvalidOperationException和ArgumentException。
我是否仅限于使用这些建议的异常类型,或者我是否可以自由使用除指南中建议的异常类型之外的全部范围?
弑天下
眼眸繁星
相关分类