Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / Visual Basic

Configuring Exceptions Using Web.config

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
22 Apr 20043 min read 59.2K   202   16  
An article on handling exceptions using custom handlers, by a custom configuration section, making it independent from using the application.
Option Strict On
Imports System
Imports System.Collections
Imports System.Xml
Imports System.Configuration
Imports System.Reflection


Public Class ExceptionManagersHandler
    Implements IConfigurationSectionHandler



    Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As XmlNode) As Object Implements IConfigurationSectionHandler.Create

        Dim _HandlerBucket As New Hashtable()
        Dim appName, exceptionName, redirectUrl, errorHandler, assemblyName As String
        Dim managerNode, exceptionNode, handlerNode As XmlNode
        Dim managersNL, exceptionsNL, handlersNL As XmlNodeList
        Dim exceptionsList As Hashtable
        Dim excepInfo As ExceptionInfo

        managersNL = section.SelectNodes("//exceptionManager")
        For Each managerNode In managersNL
            appName = managerNode.Attributes.GetNamedItem("application").Value
            exceptionsNL = managerNode.SelectNodes("exceptions/exception")
            For Each exceptionNode In exceptionsNL
                exceptionsList = New Hashtable()
                exceptionName = exceptionNode.Attributes.GetNamedItem("name").Value
                redirectUrl = exceptionNode.Attributes.GetNamedItem("url").Value
                excepInfo = New ExceptionInfo(appName, exceptionName, redirectUrl)
                handlersNL = exceptionNode.SelectNodes("exceptionHandler")
                For Each handlerNode In handlersNL
                    assemblyName = handlerNode.Attributes.GetNamedItem("assembly").Value
                    errorHandler = handlerNode.Attributes.GetNamedItem("name").Value
                Next
                excepInfo.HandlerAssembly = assemblyName
                excepInfo.HandlerName = errorHandler
                exceptionsList.Add(exceptionName, excepInfo)
            Next
            _HandlerBucket.Add(appName, exceptionsList)
        Next
        Return _HandlerBucket
    End Function


    Public Shared Function PublishException(ByVal appName As String, ByVal e As Exception) As String
        If (appName Is Nothing) Then
            appName = "AccountsCentre"
        End If


        Dim expName As String = e.GetType.ToString
        Dim handlerBucket As Hashtable = CType(System.Configuration.ConfigurationSettings.GetConfig("exceptionManagers"), Hashtable)
        Dim exceptionHT As Hashtable = CType(handlerBucket.Item(appName), Hashtable)
        Dim excepInfo As ExceptionInfo
        If Not (exceptionHT Is Nothing) Then
            excepInfo = CType(exceptionHT.Item(expName), ExceptionInfo)
            Dim assemName, className As String
            Dim classType As Type

            Try
                assemName = excepInfo.HandlerAssembly
                className = assemName & "." & excepInfo.HandlerName
                Dim assem As [Assembly] = [Assembly].Load(assemName)
                classType = assem.GetType(className)
                Dim objref As Object = Activator.CreateInstance(classType)
                Dim handler As IExceptionHandler = CType(objref, IExceptionHandler)
                handler.executeHandler(e)

            Catch exp As Exception

            End Try
        End If
        Return excepInfo.RedirectUrl
    End Function



End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
I am an IT graduate , and have secured SCPJ 2 and IBM XML developer cerifications.Started my career as J2EE developer, but now focused on .Net technologies.Currently working as a web developer ,in infinilogic (www.infinilogic.com),a UK based organization , providing business solutions.

Comments and Discussions