.NET:丢失所需的配置设置时抛出哪个异常? [英] .NET: Which Exception to Throw When a Required Configuration Setting is Missing?

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

问题描述

以下是一个标准方案:

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

问题是,我不完全确定 $ c> SomeStandardException 应该是。



我仔细阅读了3.5框架,发现了两个可能的候选人: ConfigurationException ConfigurationErrorsException



< >

System.Configuration.ConfigurationException




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

备注


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

注意



ConfigurationException object为
,以保持向后兼容性。
ConfigurationErrorsException
对象替换
配置系统。


这个异常实际上听起来非常适合我需要的东西,但它已经被标记为过时了,所以,ixnay在atthay。



彻底令人费解的 ConfigurationErrorsException


System.Configuration.ConfigurationErrorsException



当前值不是
EnableSessionState值中的一个。


正如你所看到的,它的文档完全没用。 (在本地和在线帮助中都是这样。)对类本身的检查显示,对于我想要的是非常过分。



简而言之,我需要一个标准异常,应用程序配置设置丢失或包含无效值时应抛出该异常。你会认为框架有一个例外,它为应用程序使用。 (它显然是,但它被标记为过时,并被范围更大的很大替换。)





编辑附录



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



这也值得注意的是,我主要工作的应用程序是一个控制台应用程序运行在批处理模式,我希望它抛出一个异常被捕获主要方法并记录适当地如果事情没有适当配置。 (这是我继承的遗产代码,目前只是假定所有内容都是peachy。)

解决方案

不限于在你的异常抛出到框架中的现有异常。如果你决定使用现有的异常,你不必绝对遵循文件的信。本文档将介绍框架如何使用给定的异常,但不意味着对如何选择使用/重用现有异常的任何限制。



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

  [Serializable] 
public class ConfigurationMissingException:ConfigurationErrorsException
{}

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



更新:我同意评论,所以我已经从异常更改了子类ConfigurationErrorsException。我认为通常是一个好主意,从现有的框架异常的子类化自定义异常,尽可能避免异常类,除非你需要一个应用程序特定的异常。


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