0

The constructor of an exception should be allowed to throw exceptions?

Example:

public class MyException : Exception
{
   public List<string> Errors { get; private set; }

   public MyException(List<string> errors)
   {
      if(errors == null)
      {
         throw new ArgumentNullException();
      }
      else if(errors.Count == 0)
      {
         throw new ArgumentException("At least one error must be present");
      }
      Errors = errors;
   }
}

Those exceptions (ArgumentNullException and ArgumentException) thrown in the constructor can do harm while using MyException?

This is use case of this exception:

class Program
{
    private static void ErrorHandling()
    {
        List<string> lst = new List<string>();

        // ... here do some checks and insert the errors in the list 'lst'

        // This check prevent the ArgumentNullException and
        // the ArgumentException to be thrown
        if (lst.Count > 0) 
        {
            throw new MyException(lst);
        }
    }

    static void Main(string[] args)
    {
        try
        {
            ErrorHandling();
        }
        catch(MyException e)
        {
            foreach(string s in e.Errors)
            {
                Console.WriteLine(s);
            }
        }
    }
}

The harm I talk about is: if for some reasons the programmer that use MyException doesn't check the input list (if (lst.Count > 0)) it can lead to an unwanted ArgumentNullException/ArgumentException.

I think this can lead to bugs where the programmer is trying to throw MyException with wrong parameters but instead it throw an ArgumentNullException/ArgumentException.

Should I:

  • Don't do checks in the constructors of the MyException and leave the managment of the Errors property entirely by the user
  • Do the checks and throw ArgumentNullException/ArgumentException, knowing that this can lead bugs
Simone
  • 17
  • 3
  • @DaltonCézane OP is asking about throwing exceptions from the constructor of an Exception class specifically. The links you provided talk about throwing exceptions from constructors in general – Bruno Nov 05 '18 at 20:47
  • Yes, you both are right. Thank you for the warning. – Dalton Cézane Nov 05 '18 at 20:50
  • 1
    It is perfectly acceptable to throw exceptions in a constructor provided that they are **indicating that the caller has a bug**. In your case, that's exactly what you're doing. – Eric Lippert Nov 05 '18 at 21:14
  • @Bruno that would be the same in this case as well – Rahul Nov 05 '18 at 21:14
  • @Simone - This doesn't look like a duplicate of the linked question (which is for Java not c#) but it does feel primarily opinion-based as defined, say, [here](https://meta.stackoverflow.com/questions/315569/what-classifies-as-opinion-based-open-discussion), since it's basically a question about coding standards & best practices. Is there a need to re-open this? – dbc Nov 19 '18 at 20:40

1 Answers1

-1

Those exceptions can do harm while building the MyException

I don't see you are building an exception at all and moreover your MyException class makes very little sense. What you are doing can simply be done using probably another private method. Then why build a application exception for this unnecessarily?

public List<string> CheckErrors(List<string> errors)
{
      if(errors == null)
      {
         throw new ArgumentNullException()
      }
      else if(errors.Count == 0)
      {
         throw new ArgumentException("At least one error must be present");
      }

 return errors;
}
Rahul
  • 76,197
  • 13
  • 71
  • 125