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

后端 未结 11 1143
星月不相逢
星月不相逢 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:46

    I'd suck it up and roll my own... but before you do that, Is it possible to have the system assume a default value for this configuration setting? I generally attempt to do that for every setting that might get missed bu Ops Management folk... (or perhaps I should say, for as many settings as possible - for some it is clearly not appropriate to have the system make a default decision...)

    in general a custom exception is not a lot of effort... here's an example...

    [Serializable]
    public class MyCustomApplicationException : ApplicationException
    {
        #region privates
        #endregion privates
    
        #region properties
        #endregion properties
    
        public MyCustomApplicationException (string sMessage,
            Exception innerException)
            : base(sMessage, innerException) { }
        public MyCustomApplicationException (string sMessage)
            : base(sMessage) { }
        public MyCustomApplicationException () { }
    
        #region Serializeable Code
        public MyCustomApplicationException (
           SerializationInfo info, StreamingContext context)
            : base(info, context) { }
        #endregion Serializeable Code
    }
    

提交回复
热议问题