抛出TimeoutException是一个好习惯吗?

问题描述 投票:0回答:2

我正在为设备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。他们只建议使用少数异常类型,例如InvalidOperationExceptionArgumentException

我是否仅限于使用这些建议的例外类型,或者除了指南中建议的那些外,我是否可以自由使用全范围?

c# exception
2个回答
1
投票

这应该是完全可以接受的。我无法想象为什么有人会不同意。甚至有一些被视为“不良实践”的例外仍有其用途。这种做法是否可以接受是主观的,但在你的情况下我会说这很好。任何有更多经验的人都可以自由地反对。


0
投票

我个人认为抛出自定义异常比抛出标准异常(也可以通过代码中的其他区域抛出)更好,因此无法查明实际的源代码问题。

此外,我觉得应该尝试抛出异常而不是返回错误代码等(0,1等)以实现可读性和可维护性目的。本指南提供了有关最佳实践的更多信息:

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions

© www.soinside.com 2019 - 2024. All rights reserved.