Article Options
Premium Sponsor
Premium Sponsor

Structured Exception Handling in .NET (ApplicationException)
by Arnaldo Sandoval | Published  11/04/2006 | .NET Framework .NET Intermediate Windows Development | Rating:
Page 7

Now that most of the disclaimers and explanations for the connection string are covered, let's move on with the method's remaining logic. It is nothing but a plain vanilla INSERT INTO method.  It is expecting the table ErrorLog.  This table is defined with the following columns:

There is a column per each member of the Exception class we want to save into the error log. If you want to implement a different data base like MS SQL or Oracle, you should create a table named ErrorLog with the same columns, although Oracle is a little bit tricky handling the Auto Increment column.

All the columns from the ErrorLog table are surrounded with brackets to avoid reserved words exceptions. We are using parameters to assign values to each of the table's columns; it is safer this way as we don't have to worry about apostrophes in the data.

The method relies on the OleDB namespace, so we are importing it at the top of the class file, as previously explained.

TIP: If you expand this class to support MS SQL or Oracle, you should import the appropiate namespaces.  The INSERT INTO command is basically the same for Access and MS SQL, but with Oracle you should replace the '@' charater with ":" (colons) as that is Oracle's preferred parameters leading character.

Public GetExceptions() Method:
This method returns a data table that you can bind to a data grid, providing an easy way to look at all the exceptions written to the ErrorLog table.

     Public Function GetExceptions(ByVal ConnectionStringKey As String) As DataTable

        Dim SQL_Connection As New OleDbConnection
        Dim SQL_Command As New OleDbCommand
        Dim sSQL_Command As String = ""
        Dim SQL_Adapter As New OleDbDataAdapter
        Dim dt As New DataTable
        ' Retrieve the connection string, from config file, pass it to the command object
        If ConnectionStringKey.Length = 0 Then
            ConnectionStringKey = DEFAULT_CONNECTION_STRING
        End If

        SQL_Connection.ConnectionString = AppSettings.Get(ConnectionStringKey)
        SQL_Command.Connection = SQL_Connection

        sSQL_Command = "SELECT * FROM ErrorLog"
        SQL_Command.CommandText = sSQL_Command

            SQL_Adapter.SelectCommand = SQL_Command
            GetExceptions = dt

        Catch exc As OleDbException
            GetExceptions = Nothing

        End Try


        SQL_Connection = Nothing
        SQL_Adapter = Nothing

End Function

ConnectionStringKey: This parameter has the same scope as the one used at the SaveToAccess method, all the disclaimers and explanations written before.

This method is good to obtain all the exception details from an Access database, small changes are required when dealing with MS SQL and Oracle.

ApplicationException Class by MSDN.
ApplicationException Members by MSDN.
.NET Anatomy - Structured Exception Handling in .NET by Barton Friedland.
Exception classes in .NET Framework
Exceptions by Terry Smith.
The Well-Tempered Exception by Eric Gunnerson at Microsoft Corporation
How to write custom exception classes in C#

The code is posted and attached as is, feel free to use it, we advise that you should get acquainted with it before implementing this class in your applications.

Its main weakness is with the WriteToFile method; you should review and perhaps enhance it, driven by your organization system security rules.

The SaveToAccess() method does not have exception trapping and connection string key validations.  You may add this validation or go ahead knowing the slim chance it may break at those two danger points.
The attached zip file contains the class project ErrorHandlingExceptions.   Its Data folder contains the access data base the test project uses to write the exceptions details.  You should either copy this Access database to your C:\Temp folder, or amend the connection string making it to point to the folder of your preference.

The project is written in VB.Net 2003.



Sponsored Links