ConfigurationErrorsException Class

Note: This class is new in the .NET Framework version 2.0.

The exception that is thrown when a configuration-system error has occurred.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

'Declaration
<SerializableAttribute> _
Public Class ConfigurationErrorsException
	Inherits ConfigurationException
'Usage
Dim instance As ConfigurationErrorsException

/** @attribute SerializableAttribute() */ 
public class ConfigurationErrorsException extends ConfigurationException
SerializableAttribute 
public class ConfigurationErrorsException extends ConfigurationException

The ConfigurationErrorsException exception is thrown when any error occurs while configuration information is being read or written.

The following code example creates a custom section and generates a ConfigurationErrorsException exception when modifying its properties.

Imports System
Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections



' Define a custom section.

NotInheritable Public Class CustomSection
    Inherits ConfigurationSection
    
    Public Sub New() 
    
    End Sub 'New
    
    
    <ConfigurationProperty("fileName", DefaultValue:="default.txt", IsRequired:=True, IsKey:=False), StringValidator(InvalidCharacters:=" ~!@#$%^&*()[]{/;'""|\", MinLength:=1, MaxLength:=60)> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
            Me("fileName") = value
        End Set
    End Property
    
    
    <ConfigurationProperty("maxUsers", DefaultValue:=10, IsRequired:=False), LongValidator(MinValue:=1, MaxValue:=100, ExcludeRange:=False)> _
    Public Property MaxUsers() As Long
        Get
            Return Fix(Me("maxUsers"))
        End Get
        Set(ByVal value As Long)
            Me("maxUsers") = value
        End Set
    End Property
End Class 'CustomSection 


' Create the custom section and write it to
' the configuration file.

Class UsingConfigurationErrorsException
    
    ' Create a custom section.
    Shared Sub New()

        ' Get the application configuration file.
        Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

        ' If the section does not exist in the configuration
        ' file, create it and save it to the file.
        If config.Sections("CustomSection") Is Nothing Then
            Dim custSection As New CustomSection()
            config.Sections.Add("CustomSection", custSection)
            custSection = config.GetSection("CustomSection")
            custSection.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
        End If

    End Sub 'New
    
    
    ' Modify a custom section and cause configuration 
    ' error exceptions.
    Shared Sub ModifyCustomSection() 
        
        Try
            ' Get the application configuration file.
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            Dim custSection _
            As CustomSection = _
            config.Sections("CustomSection")
             
            ' Change the section properties.
            custSection.FileName = "newName.txt"
            
            ' Cause an exception.
            custSection.MaxUsers = _
            custSection.MaxUsers + 100
            
            If Not custSection.ElementInformation.IsLocked Then
                config.Save()
            Else
                Console.WriteLine( _
                "Section was locked, could not update.")
            End If
        Catch err As ConfigurationErrorsException
            
            Dim msg As String = err.Message
            Console.WriteLine("Message: {0", msg)
            Dim fileName As String = err.Filename
            Console.WriteLine("Filename: {0", _
            fileName)
            Dim lineNumber As Integer = err.Line
            Console.WriteLine("Line: {0", _
            lineNumber.ToString())
            Dim bmsg As String = err.BareMessage
            Console.WriteLine("BareMessage: {0", bmsg)
            Dim src As String = err.Source
            Console.WriteLine("Source: {0", src)
            Dim st As String = err.StackTrace
            Console.WriteLine("StackTrace: {0", st)
        End Try

    End Sub 'ModifyCustomSection

    Shared Sub Main(ByVal args() As String) 
        ModifyCustomSection()
    
    End Sub 'Main
End Class 'UsingConfigurationErrorsException 


The following configuration excerpt is used by the previous example.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="CustomSection" type="Samples.AspNet.CustomSection, 
      ConfigurationErrorsException, Version=1.0.0.0, Culture=neutral, 
      PublicKeyToken=null" allowDefinition="Everywhere" 
      allowExeDefinition="MachineToApplication" 
      restartOnExternalChanges="true" />
  </configSections>
  <CustomSection fileName="default.txt" maxUsers="10" />
</configuration>

System.Object
   System.Exception
     System.SystemException
       System.Configuration.ConfigurationException
        System.Configuration.ConfigurationErrorsException

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show: