Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception to throw when expecting a null value?

Tags:

c#

exception

If I am expecting a null value and get a defined value (within a getter of a property) and want to throw an exception, what would be the proper way to do this in csharp? Is there anything defined already that makes sense in this situation?

like image 399
Joe Avatar asked Jul 24 '26 15:07

Joe


2 Answers

My guess would be:

throw new
    ArgumentException("Parameter was expected to be null, value was provided.");

ArgumentOutOfRangeException might also work, but is typically used when there is a well defined range rather than null vs. not null.

like image 91
Justin Niessner Avatar answered Jul 26 '26 04:07

Justin Niessner


I would probably use ArgumentOutOfRangeException

like image 34
AllenG Avatar answered Jul 26 '26 04:07

AllenG