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

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

问题描述

下面是一个标准的情况:

Here's a standard scenario:

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

现在的问题是,我不能完全肯定的的异常 SomeStandardException 应。

我细读了3.5框架,并发现了两个可能的候选人:<一href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationexception.aspx"><$c$c>ConfigurationException和<一href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationerrorsexception.aspx"><$c$c>ConfigurationErrorsException.

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.

说明

的    ConfigurationException的的例外是   如果应用程序试图抛出   读取或写入数据到   配置文件,但   不成功。一些可能的原因   这可以包括在正常的XML   配置文件,文件   权限问题,和配置   是不与属性值   有效的。

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.

注意:

ConfigurationException的对象   保持向后兼容性。   该 ConfigurationErrorsException   对象取代它的   配置系统。

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

这异常实际上听起来非常适合我的需要,但它已经标记为已过时,因此,ixnay上atthay。

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

这给我们带来了彻底的令人费解的<一个href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationerrorsexception.aspx"><$c$c>ConfigurationErrorsException:

This brings us to the thoroughly puzzling ConfigurationErrorsException:

的电流值是不是一个   的EnableSessionState值。

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?

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

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.

另外,值得一提的是,该应用程序我主要工作是在批处理模式下运行的控制台应用程序,我希望它抛出被捕获的主要方法和记录适当的异常,如果事情是不适当配置。 (它的遗留code我继承,目前只的假设的一切都桃色的。)

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.

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

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
{}

编辑:在这种情况下,编写自己的异常进行低保永远不会有关于那里的异常即将从 - 框架,或者应用程序的任何混乱的好处。该框架将不会抛出您的自定义异常。

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.

更新:我同意的意见,所以我的子类的异常改变ConfigurationErrorsException。我认为这是通常的子类的现有框架例外情况可能自定义异常,避免异常类,除非你需要的应用程序特定的异常是一个好主意。

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天全站免登陆