Programátorská hádanka: ArgumentException, ArgumentNullException

Přijde vám na následujícím kódu něco divného?

public void DoSomething(MyClass firstParameter)
{
  if (firstParameter == null)
  {
    throw new ArgumentNullException(nameof(firstParameter), "My message.");
  }
  if (firstParameter.SomeProperty < 0)
  {
    throw new ArgumentException(nameof(firstParameter), "My other message.");
  }

  // do something...
}

4 thoughts on “Programátorská hádanka: ArgumentException, ArgumentNullException

  1. Jan Holan

    ArgumentException má parametry (String message, String paramName).
    Jinak já osobě u ArgumentNullException používám vždy variantu pouze s parametrem paramName, protože to už říká samo osobě vše potřebné, ta message je u ArgumentNullException zbytečná.

    To se mi líbí

    1. Jan Holan

      Naopak vyhazování ArgumentNullException je hodně dobrá (docela rád bych to někomu zaved jako povinné) praktika, protože právě NullReferenceException neříká tu informaci, který objekt je null.

      To se mi líbí

Napsat komentář