What's the best exception to throw if the output of the external command is meaningless?

I am developing a C # utility class that runs an external command on a Linux server over SSH (I use SharpSSH , I like it and I recommend it) and returns some meaningful information based on the output of the command.

There is an integer somewhere in the above release that should never be out of range, but I would like to prevent this unlikely possibility. Which exception is more correct to throw since the resulting number is actually out of range? I think ArgumentOutOfRangeException

, but that's not an argument. Autocomplete doesn't give me good candidates. Any suggestions?

+2


source to share


3 answers


The thrown exception type is applicable only for two reasons:

  • You (or your consumers) intend to catch the exception in certain cases and somehow react to it programmatically in the catch () block.
  • You need some extra clarity for the caller when a specific problem arises - not even a programmatic response can be programmed to it.


So, to answer your question,

  • if you think consumers of this code might want to catch this particular exception and handle it somehow, then throw a custom exception inheriting from Exception

    with the necessary granularity.
  • If you do not think that handling or responding to this exception would be helpful, consider either ApplicationException

    , or possible InvalidOperationException

    for the type of error described.
+2


source


I suggest InvalidOperationException . Another option is NotSupportedException , if it is possible that the value goes into potentially usable ranges (say in the future).

Ultimately I would suggest:



namespace YourProtocol
{
    public class UnexpectedProtocolValueException : Exception
    {

      

+1


source


You can find a complete list of .NET Exceptions on Mike Walloff's blog - that should help you decide. Otherwise, you can always create your own exception class .

+1


source







All Articles