我正在为设备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}.")
{
}
/// <summary>
/// Initializes a new instance of the <c>FanucLib.FanucTimeoutException</c> class with
/// a specified error message and a reference to the inner exception that is the
/// cause of this exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception. If the innerException
/// parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
public FanucTimeoutException(string message, Exception innerException) : base(message, innerException)
{
}
/// <summary>
/// Initializes a new instance of the <c>FanucLib.FanucTimeoutException</c> class with
/// a specified error message, the address trying to access and a reference to the inner exception that is the
/// cause of this exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="address">The address trying to access.</param>
/// <param name="innerException">The exception that is the cause of the current exception. If the innerException
/// parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
public FanucTimeoutException(string message, string address, Exception innerException) : base($"{message} Address: {address}.", innerException)
{
}
/// <inheritdoc />
public FanucTimeoutException(SerializationInfo info, StreamingContext context)
{
}
}
但是,我不确定抛出TimeoutException
是否是一个好习惯。关于异常设计的C# guide和.Net guide都没有谈论TimeoutException
。他们仅建议使用少数异常类型,例如InvalidOperationException
和ArgumentException
。
我仅限于使用这些建议的异常类型,还是可以自由使用全部范围,但指南中建议的除外?
答案 0 :(得分:1)
这应该是完全可以接受的。我无法想象为什么会有人不同意。甚至还有一些被认为是“不良做法”的例外情况仍然可以使用。这种做法是否可以接受是主观的,但是在您的情况下,我会说很好。任何有更多经验的人都可以不同意。
答案 1 :(得分:0)
我个人认为抛出自定义异常比抛出标准异常(也可以通过代码的其他区域抛出)更好,因此无法查明实际的源代码问题。
此外,我觉得为了可读性和可维护性,应该尝试引发异常,而不是返回错误代码等(0、1等)。本指南提供了有关最佳做法的更多信息:
https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions