DavidB
DavidB

Reputation: 2596

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. These could potentially be null, and I would like to throw an exception if this is the case.

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.

Is there an alternative, or is this not a good approach?

Upvotes: 7

Views: 7707

Answers (5)

mnieto
mnieto

Reputation: 3874

You can use any of the System.Configuration namespace exceptions. Or create your own ConfigurationParameterNullException

Upvotes: 2

NikolaiDante
NikolaiDante

Reputation: 18649

You can create your own custom exception (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(); as that would be a violation of CA2201: Do not raise reserved exception types:

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.

Instead, either throw a more derived type that already exists in the framework, or create your own type that derives from 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)
    {
    }
}

Upvotes: 4

Hans Passant
Hans Passant

Reputation: 942187

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. 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. And that he can read it, write an event handler for AppDomain.CurrentDomain.UnhandledException

Upvotes: 9

J Burnett
J Burnett

Reputation: 26

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). If you take the generic approach, be sure to add details in Message, Data, etc.

Upvotes: 0

Jamadan
Jamadan

Reputation: 2313

Create a class that inherits from Exception:

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

Upvotes: 1

Related Questions