Table of Contents

ConfigurationParseException Class

Definition

Namespace
Bodu.Text.Configuration
Assembly
Bodu.Text.Configuration.dll
Package
Bodu.Text.Configuration 1.3.0
Source
ConfigurationParseException.cs

The exception raised when a configuration document cannot be parsed or resolved. Exposes the diagnostic that stopped the operation as Diagnostic; the library raises it with that diagnostic alone.

public sealed class ConfigurationParseException : FormatException, ISerializable
Inheritance
ConfigurationParseException
Implements
Inherited Members
Extension Methods

Examples

try
{
    ConfigurationDocument doc = ConfigurationDocument.Parse(text);
}
catch (ConfigurationParseException ex)
{
    // The primary diagnostic includes a source location that points back into the document.
    Console.WriteLine($"Parse failed at {ex.Location}: {ex.Message}");

    // Diagnostics holds the diagnostic that stopped the parse.
    foreach (ConfigurationDiagnostic d in ex.Diagnostics)
        Console.WriteLine($"  {d}");
}

Remarks

In Throw mode the parser raises this exception on the first recoverable error it encounters. In Collect and Ignore modes parsing never raises it: errors surface on the Diagnostics list or are dropped, and an I/O error from the source propagates unchanged. Resolving a document raises it too, when a section name is not a valid glob (see Compile(string, StringComparison)).

Every exception the library raises carries exactly one diagnostic, the error that stopped the operation, as Diagnostic and as the single entry of Diagnostics; warnings the parser reported before the error are not attached. Diagnostics holds more than one entry only when a caller constructs the exception from a list with ConfigurationParseException(IReadOnlyList<ConfigurationDiagnostic>), and is empty, but never default, when it is constructed from a message alone. Location forwards to the primary diagnostic's location and falls back to None when no diagnostic is attached.

Constructors

ConfigurationParseException()

Initializes a new instance of the ConfigurationParseException class with a default message.

public ConfigurationParseException()

Remarks

The default message is read from the library's resources for the current UI culture.

ConfigurationParseException(ConfigurationDiagnostic)

Initializes a new instance of the ConfigurationParseException class from a single diagnostic. The diagnostic is exposed both via Diagnostic and as the single entry of Diagnostics.

public ConfigurationParseException(ConfigurationDiagnostic diagnostic)

Parameters

diagnostic ConfigurationDiagnostic

The diagnostic that triggered the failure.

Exceptions

ArgumentNullException

diagnostic is null.

ConfigurationParseException(IReadOnlyList<ConfigurationDiagnostic>)

Initializes a new instance of the ConfigurationParseException class from a set of previously collected diagnostics. The first entry, if any, is exposed via Diagnostic.

public ConfigurationParseException(IReadOnlyList<ConfigurationDiagnostic> diagnostics)

Parameters

diagnostics IReadOnlyList<ConfigurationDiagnostic>

The diagnostics the exception carries.

Remarks

The library itself never uses this constructor; it is for a caller that collects diagnostics, for example with Collect, and reports them together.

Exceptions

ArgumentNullException

diagnostics is null.

ConfigurationParseException(string)

Initializes a new instance of the ConfigurationParseException class with the specified message.

public ConfigurationParseException(string message)

Parameters

message string

The message describing the failure.

ConfigurationParseException(string, Exception?)

Initializes a new instance of the ConfigurationParseException class with the specified message and inner exception.

public ConfigurationParseException(string message, Exception? innerException)

Parameters

message string

The message describing the failure.

innerException Exception

The exception that caused the failure.

Properties

Diagnostic

Gets the primary diagnostic that triggered this exception, or null if the exception was constructed without one.

public ConfigurationDiagnostic? Diagnostic { get; }

Property Value

ConfigurationDiagnostic

The primary diagnostic, or null.

Diagnostics

Gets the diagnostics the exception carries: the single diagnostic that stopped the operation when the library raises it, or the list a caller supplied. Always returns a non-default array; may be empty.

public ImmutableArray<ConfigurationDiagnostic> Diagnostics { get; }

Property Value

ImmutableArray<ConfigurationDiagnostic>

An immutable, possibly empty list of diagnostics.

Location

Gets the location in the source document that triggered the exception.

public ConfigurationSourceLocation Location { get; }

Property Value

ConfigurationSourceLocation

The associated ConfigurationSourceLocation, or None when no primary diagnostic is available.

Applies to

ProductVersions
.NET8, 10