Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to create my own InvalidArgumentException.. I couldn't find any builtin type in c#

Tags:

c#

exception

Do I need to create my own InvalidArgumentException.. I couldn't find any built-in types in c#... Is there any library which defines commonly used Exception classes.. Thanks

like image 953
StackUnderflow Avatar asked Jan 26 '09 20:01

StackUnderflow


2 Answers

It's called ArgumentException and it's in the built-in System namespace.

like image 122
Vojislav Stojkovic Avatar answered Nov 05 '22 06:11

Vojislav Stojkovic


Besides ArgumentException there are also ArgumentNullException and ArgumentOutOfRangeException.

Short summary;

  • System.ArgumentException - general exception,
  • System.ArgumentNullException - unexpected null value,
  • System.ArgumentOutOfRangeException - unexpected value.

Standard usage:

void GetEntity(string currentName)
{
  if (String.IsNullOrEmpty(currentName))
  {
    throw new ArgumentNullException(nameof(currentName));
  }

  //...
}

PS. NotSupportedException also comes in handy (e.g. when the set of arguments would force the method to perform not supported scenario).

like image 43
andrew.fox Avatar answered Nov 05 '22 05:11

andrew.fox