Should I Throw ArgumentNullException if a string is blank?

前端 未结 7 1738
独厮守ぢ
独厮守ぢ 2020-12-14 00:11

I working on a method that does something given a string parameter. A valid value for the string parameter is anything other than null or string.Empty. So my code looks li

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 00:24

    This depends on circumstance really.

    The question comes down to, is it really an error? By that I mean do you always expect a value? If you do, then probably your best bet here is creating your own Exception, perhaps like so:

    class StringEmptyOrNullException : Exception
    {
    }
    

    Where you can also add your own constructors and added information etc.

    If it however is not an "exceptional" happening in your program, if would probably be a better idea to return null from the method and handle it from there. Just remember, Exception's are for exceptional conditions.

    Hope this helps,

    Kyle

提交回复
热议问题