How to pass ArgumentNullException before calling another constructor

I have a question related to constructor syntax and throwing exceptions inside a constructor.

How can I pass ArgumentNullException for argument b before calling CreateAnotherOne () and throw an exception like in the second constructor without duplicating code after checks? I could extract the code into a separate private method, but I need to call it from both constructor bodies ... is there any other option for this?

public class MyClass
{
    public MyClass(IWhatEver a, ISomeThingElse b)
        : this(a, b != null ? b.CreateAnotherOne() : null)
    {
        // or should I call :this(a, b.CreateAnotherOne()) instead? this could cause a NullReferenceException => how to verify that b is not null?
        // don't want to call CallMeFromConstructor() instead of call to other constructor

        // would not do the following (why should I check a/c twice?) and the check is too late here (because already called a method on b, b.CreateAnotherOne())
        if (a == null)
        {
            throw new ArgumentNullException("a");
        }

        if (b == null)
        {
            throw new ArgumentNullException("b");
        }
    }

    public MyClass(IWhatEver c, IAnotherOne d)
    {
        if (c == null)
        {
            throw new ArgumentNullException("c");
        }
        if (d == null)
        {
            throw new ArgumentNullException("d");
        }

        // the cool code comes here, I could put it into
        // the CallMeFromConstructor method but is there another way?
    }
    ...

    private void CallMeFromConstructors()
    {
        // the cool code could be here, too (but is there another way?)
    }

      

If I call the second constructor with: this (a, b! = Null? B.CreateAnotherOne (): null) I would get an ArgumentNullException for d int in the second constructor. This sounds strange to me and can be confusing because I named the first one (this can only be seen in the stack trace).

The problem is that I cannot write

:this(a, b == null ? b.CreateAnotherOne() : throw new ArgumentNullException("b"));

      

and if I put a check in the body of the constructor, it will be checked to the end in this case.

Any syntactic sugar ideas to solve this problem?

+3


source to share


1 answer


A private method will do it, but you can make another private constructor:



    private MyClass(IWhatEver a)
    {
        if (a == null)
        {
            throw new ArgumentNullException("a");
        }

        // the cool code comes here, I could put it into
        // the CallMeFromConstructor method but is there another way?
    }

    public MyClass(IWhatEver a, ISomeThingElse b) : this(a)
    {
        if (b == null)
        {
            throw new ArgumentNullException("b");
        }
    }

    public MyClass(IWhatEver a, IAnotherOne b) : this(a)
    {
        if (b == null)
        {
            throw new ArgumentNullException("b");
        }
    }

      

+3









All Articles