.NET:当缺少必需的配置设置时,哪个异常会被抛出? [英] .NET: Which Exception to Throw When a Required Configuration Setting is Missing?

查看:162
本文介绍了.NET:当缺少必需的配置设置时,哪个异常会被抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个标准场景:

  if(string.IsNullOrEmpty(Configuration.AppSettings [foobar]))
throw new SomeStandardException(应用程序未正确配置,bozo);

问题是,我不完全确定哪个异常 SomeStandardException 应该是。



我习惯了3.5框架,发现两个可能的候选人: ConfigurationException ConfigurationErrorsException


System.Configuration.ConfigurationException



发生
配置系统错误
时抛出的异常。

备注


ConfigurationException 异常是
抛出,如果应用程序尝试到
读取或写入数据到
配置文件,但
不成功。一些可能的原因
可能包括配置文件
中的格式错误的XML文件,
权限问题,以及配置
属性,值为
有效。 p>

注意:



ConfigurationException 对象是维持向下兼容的

ConfigurationErrorsException
对象替换
配置系统。


这个例外实际上对我需要的是完美的,但是它已被标记为过时,所以ixnay onthay。



这带给我们完全令人困惑的 ConfigurationErrorsException


System.Configuration.ConfigurationErrorsException



当前值不是
EnableSessionState值之一。


如您所见,其文档完全无用。 (这是在本地和在线帮助的方式。)对班级本身的考察表明,这是我想要的激烈的过度杀戮。



简而言之,我需要一个标准异常,当应用程序配置设置丢失或包含无效值时,应该抛出该异常。你会认为框架有这样一个例外,以供应用程序使用。 (显然,这显然已经过时了,而且被范围更大的东西所替代。)



你们是否为此而使用,而且我将不得不吸吮它,并为此自行提出例外?



编辑附录


有些人问我是否可以提供一个默认值,然后继续。在某些情况下,是的,在这种情况下,不会抛出异常。但是,对于某些设置,这不适用。例如:数据库服务器名称和凭据,身份验证服务器和安装的第三方应用程序的路径。



值得注意的是,我主要使用的应用程序是以批处理模式运行的控制台应用程序,我希望它抛出一个被主要方法,如果事情没有正确配置,则适当地记录。 (这是我继承的遗留代码,目前只是假定所有的东西都是桃红色。)

解决方案

你在您的异常中不受限制 - 抛出框架中的现有异常。如果您决定使用现有的异常,那么您绝对不必按照该文档的说明进行操作。文档将描述框架如何使用给定的异常,但并不意味着您 选择使用/重用现有异常的任何限制。



这是您的应用程序 - 只要您记录它,并清楚地指出将在丢失配置值的特定情况下抛出的异常,则可以使用任何异常喜欢。如果你想要一个非常具体的缺少值的指示,你可能会考虑编写自己的ConfigurationSettingMissing异常:

  [Serializable] 
public class ConfigurationMissingException:ConfigurationErrorsException
{}

编辑:在这种情况下编写自己的异常,带来额外的好处,即保证在框架或应用程序的异常发生的地方永远不会有任何混淆。框架永远不会抛出您的自定义异常。



更新:我同意这些注释,所以我已经将子类从Exception更改为ConfigurationErrorsException。我认为在可能的情况下将现有Framework异常中的自定义异常子类化一般是个好主意,避免使用Exception类,除非需要特定于应用程序的异常。


Here's a standard scenario:

if(string.IsNullOrEmpty(Configuration.AppSettings["foobar"]))
   throw new SomeStandardException("Application not configured correctly, bozo.");

The problem is, I am not entirely certain which exception SomeStandardException should be.

I perused the 3.5 Framework and found two likely candidates: ConfigurationException and ConfigurationErrorsException.

System.Configuration.ConfigurationException

The exception that is thrown when a configuration system error has occurred.

Remarks

The ConfigurationException exception is thrown if the application attempts to read or write data to the configuration file but is unsuccessful. Some possible reasons for this can include malformed XML in the configuration file, file permission issues, and configuration properties with values that are not valid.

Note:

The ConfigurationException object is maintained for backward compatibility. The ConfigurationErrorsException object replaces it for the configuration system.

This exception actually sounds perfect for what I need, but it's been marked obsolete, so, ixnay on atthay.

This brings us to the thoroughly puzzling ConfigurationErrorsException:

System.Configuration.ConfigurationErrorsException

The current value is not one of the EnableSessionState values.

As you can see, its documentation is completely useless. (It's that way in both local and online help.) An examination of the class itself shows that it's drastic overkill for what I want.

In a nutshell, I need a standard exception that should be thrown when an application configuration setting is missing or contains an invalid value. You'd think the Framework had such an exception baked into it for applications to use. (It apparently did, but it was marked obsolete, and was replaced by something much larger in scope.)

What solutions, if any, are you guys using for this, and am I going to have to suck it up and roll my own exception for this?

Edit Addenda

Some have asked whether or not I could provide a default value, and continue. In certain cases, yes, and in those cases, the exception would not be thrown. However, for certain settings, this won't apply. For instance: database server names and credentials, authentication servers, and paths to installed third party applications.

It is also worth noting that the application I'm primarily working on is a console application running in batch mode, and I want it to throw an exception that is caught by the main method and logged appropriately if the thing isn't appropriately configured. (It's legacy code I've inherited, and currently just assumes everything's peachy.)

解决方案

You're not limited in your exception-throwing to existing exceptions in the Framework. If you do decide to use existing exceptions, you don't absolutely have to follow the documentation to the letter. The documentation will describe how the framework uses a given exception, but doesn't imply any limitation on how you choose to use/reuse an existing exception.

It's your application- as long as you document it and clearly indicate the exception that will be thrown in the specific case of a missing configuration value, you can use any exception you like. If you do want a very specific indication of a missing value, you might consider writing your own ConfigurationSettingMissing exception:

[Serializable]
public class ConfigurationMissingException : ConfigurationErrorsException
{}

EDIT: Writing your own exception in this case carries the added benefit of guaranteeing that there will never be any confusion regarding where the exception is coming from- the framework, or your application. The framework will never throw your custom exceptions.

UPDATE: I agree with the comments, so I have changed the subclass to ConfigurationErrorsException from Exception. I think it's generally a good idea to subclass custom exceptions from existing Framework exceptions where possible, avoiding the Exception class unless you need an application-specific exception.

这篇关于.NET:当缺少必需的配置设置时,哪个异常会被抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆