简体   繁体   English

如果缺少配置值,我应该抛出什么异常?

[英]What exception should I throw if a configuration value is missing?

I have a static class that retrieves values from app.config in its constructor. 我有一个静态类,在其构造函数中从app.config中检索值。 These could potentially be null, and I would like to throw an exception if this is the case. 这些可能是null,如果是这种情况我想抛出异常。

I would like to throw the error in the static methods that use these values, but ArgumentNullException doesn't seem right as they are not arguments passed to the method. 我想在使用这些值的静态方法中抛出错误,但ArgumentNullException似乎不正确,因为它们不是传递给方法的参数。

Is there an alternative, or is this not a good approach? 有替代方案,还是这不是一个好方法?

It does not matter what you throw. 扔什么都没关系。 It is not an exception that should ever be caught, catching it does not fix the .config file. 它不应该被捕获,捕获它不会修复.config文件。 Using a custom exception type only encourages a bad practice. 使用自定义异常类型只会鼓励不良做法。

So don't, just throw Exception. 所以不要,只是抛出异常。 Be sure to tell the reader what is wrong with the .config file, that's the only thing he needs to know. 一定要告诉读者.config文件有什么问题,这是他唯一需要知道的事情。 And that he can read it, write an event handler for AppDomain.CurrentDomain.UnhandledException 并且他可以阅读它,为AppDomain.CurrentDomain.UnhandledException编写一个事件处理程序

You can use any of the System.Configuration namespace exceptions. 您可以使用任何System.Configuration命名空间异常。 Or create your own ConfigurationParameterNullException 或者创建自己的ConfigurationParameterNullException

Create a class that inherits from Exception: 创建一个继承自Exception的类:

https://msdn.microsoft.com/en-us/library/87cdya3t(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/87cdya3t(v=vs.110).aspx

public class NullConfigEntryException: Exception

And then throw a NullConfigEntryException when you find an empty one 然后在找到空的时抛出NullConfigEntryException

You can create your own custom exception (MSDN). 您可以创建自己的自定义异常(MSDN)。

Basically: 基本上:

public class ConfigurationValueMissingException : Exception
{
    public ConfigurationValueMissingException()
    {
    }

    public ConfigurationValueMissingException(string message)
        : base(message)
    {
    }

    public ConfigurationValueMissingException(string message, Exception inner)
        : base(message, inner)
    {
    }
}

Why? 为什么? If you follow Microsoft Code Analysis rules, you can't simply throw new Exception(); 如果您遵循Microsoft Code Analysis规则,则不能简单地throw new Exception(); as that would be a violation of CA2201: Do not raise reserved exception types : 因为这将违反CA2201:不要提出保留的异常类型

Do Not Throw General Exceptions 不要抛出一般例外

If you throw a general exception type, such as Exception or SystemException in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle. 如果在库或框架中抛出一般异常类型(例如Exception或SystemException),它会强制消费者捕获所有异常,包括他们不知道如何处理的未知异常。

Instead, either throw a more derived type that already exists in the framework, or create your own type that derives from Exception. 相反,要么抛出已经存在于框架中的更多派生类型,要么创建自己的派生自Exception的类型。

How to Fix Violations 如何修复违规行为

To fix a violation of this rule, change the type of the thrown exception to a specific type that is not one of the reserved types. 要修复违反此规则的情况,请将抛出的异常类型更改为不属于某个保留类型的特定类型。

When to Suppress Warnings 何时抑制警告

Do not suppress a warning from this rule. 请勿禁止此规则发出警告。

This would be a custom exception that fully passes Code Analysis rules: 这将是完全传递代码分析规则的自定义异常:

using System;
using System.Runtime.Serialization;

[Serializable]
public class ConfigurationValueMissingException : Exception
{
    public ConfigurationValueMissingException()
    {
    }

    public ConfigurationValueMissingException(string message)
        : base(message)
    {
    }

    public ConfigurationValueMissingException(string message, Exception inner)
        : base(message, inner)
    {
    }

    protected ConfigurationValueMissingException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

A custom exception is the most informative, and you have control over what information it includes. 自定义异常是信息量最大的,您可以控制它包含的信息。 A more generic approach is to use ApplicationException (the most generic being Exception). 更通用的方法是使用ApplicationException(最通用的是Exception)。 If you take the generic approach, be sure to add details in Message, Data, etc. 如果采用通用方法,请务必在消息,数据等中添加详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM