.NET: Which Exception to Throw When a Required Configuration Setting is Missing?

后端 未结 11 1136
星月不相逢
星月不相逢 2020-12-23 20:18

Here\'s a standard scenario:

if(string.IsNullOrEmpty(Configuration.AppSettings[\"foobar\"]))
   throw new SomeStandardException(\"Application not configured          


        
11条回答
  •  孤独总比滥情好
    2020-12-23 20:37

    My general rule would be:

    1. If the case of the missing configuration is not very common and I believe I would never want to handle this case differently than other exceptions, I just use the basic "Exception" class with an appropriate message:

      throw new Exception("my message here")

    2. If I do want, or think there's a high probability I would want to handle this case in a different manner than most other exceptions, I would roll my own type as people have already suggested here.

提交回复
热议问题