Search code examples
c#exception

Is throwing TimeoutException a good practice?


I'm defining an exception class for a device IO class.

I'd like to throw an exception when the communication to the device is timeout. Below is what I have at the moment:

    /// <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)
        {
        }
    }

However, I'm not sure if it is a good practice to throw TimeoutException. Both C# guide and .Net guide on exception design do not talk about TimeoutException. They only suggest the use of a handful of exception types, such as InvalidOperationException and ArgumentException.

Am I limited to only using these suggested exception types or do I have the freedom to use the full range except for the ones advised against in the guide?


Solution

  • This should be perfectly acceptable. I can't imagine why anyone would disagree. There are even exceptions that are considered "Bad Practice" that still have their uses. It's subjective whether or not that practice is acceptable but in your situation I'd say it's fine. Anyone w/ more experience is free to disagree.