ArgumentException vs. ArgumentNullException?

前端 未结 4 1098
予麋鹿
予麋鹿 2021-01-03 20:13

I’m refactoring some code and adding a method which will replace a (soon-to-be) deprecated method. The new method has the following signature:

FooResult Foo         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 20:54

    This kind of depends on your tooling and how you feel about your tooling (resharper, fxcops and the like). Some static code analysis tools accept this:

    throw new ArgumentNullException(“args.Property...”,"args");
    

    and reject this

    throw new ArgumentNullException(“args.Property...”,"args.Property");
    

    So if you want to use the tooling, then assertions of null-hood against a parameter property must throw an ArgumentException

    It's also valid to just make it up as you go along. What ever communicates the right message to the maintenance developer to help him pass the parameters correctly is the correct message.

提交回复
热议问题