Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Structured Exception Handling in .NET (ApplicationException)  »  Using ErrorHandlingException Class (i)
 »  Home  »  .NET Intermediate  »  Structured Exception Handling in .NET (ApplicationException)  »  Using ErrorHandlingException Class (i)
 »  Home  »  Windows Development  »  Structured Exception Handling in .NET (ApplicationException)  »  Using ErrorHandlingException Class (i)
Structured Exception Handling in .NET (ApplicationException)
by Arnaldo Sandoval | Published  11/04/2006 | .NET Framework .NET Intermediate Windows Development | Rating:
Using ErrorHandlingException Class (i)
HOW TO USE THE ErrorHandlingException CLASS:
This topic explains with a test project (attached) how to use the ErrorHandlingException class described within this thread.

REFERENCE TO THE ErrorHandlingException CLASS:
The first thing you should do before using the ErrorHandlingException class described above is adding a reference of its DLL to the project where you want to use it.


 
  1. Expand your project's tree in the solution explorer.
  2. Right click the References node.
  3. Click on the Add Reference option.
  4. Click the Browse button.
  5. Navigate to the folder where the file ErrorHandlingException.dll is located.
  6. Select the file.
  7. Click the Open button.
  8. Click the Ok button.

 All set, you will be able to use the ErrorHandlingException class through your project's code.

REQUIRED CONNECTION STRING AT YOUR PROJECT app.config:
If you want to record your application's exceptions to an ErrorLog table as described in the post explaining the Class, its config file should have an entry like this one.

<?xml version=<?xml version="1.0" encoding="utf-8"?>
<CONFIGURATION>
   <APPSETTINGS>
      
      
      <ADD class=s color="#ff00ff" key="" font DBConnStringErrorHandlingException?<> value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\TargetErrorLog.mdb" />
   </APPSETTINGS>
</CONFIGURATION>
</APPSETTINGS>

Where the key value should be DBConnStringErrorHandlingException which is the default expected entry, or the one of your preference, while the value should point to the location of the Access database you will be using to keep your Exceptions log.

A similar approach of creating a config entry is suggested when you expand the class to save your exception to a database other than access.

On Error Resume Next EQUIVALENT USING TRY-END TRY BLOCKS:
The Try-End Try equivalent of the On Error Resume Next statement is achieved by implementing an empty CATCH block, as shown:

Dim a As Integer
Dim b As Integer = 5
Dim c As Integer = 0

Try
   a = b / c
Catch ex As Exception

End Try
'
' more code
'

The above sample code illustrates the use of a Try-Catch-End Try block emulating the On Error Resume Next statement; the error inside the Try block will not stop the method execution.

THROWING DATA DRIVEN EXCEPTION:
You application is expected to throw an ErrorHandlingException when it finds an error condition with the data your application is handling.  It should do something like this:

   Try
      Dim
a As Integer
      Dim b As Integer = 5
      Dim c As Integer = 0
      '
      If c <> 0 Then
         a = b / c
      Else
         Throw New
SEHComponent.ErrorHandlingException( _
                                 "Doing some calculations", _
                                 "Invalid parameter")
      End If
   Catch
ehe As SEHComponent.ErrorHandlingException
        ehe.Save(SEHComponent.ErrorHandlingException.TargetErrorLogs.Access, "")
   End Try

The example code shows that the application identifies a data error condition, throwing the ErrorHandlingException.  The Try block catch it with the variable ehe and Saves its details to the target access database without showing any warning message to the user.

It will be up to the application logic to determine what to do when the error condition is found after catching the thrown exception.

HANDLING SYSTEM EXCEPTIONS:
Our ErrorHandlingException class is cool to trap any exception thrown by the application upon performing data validation, but it will completely miss any system generated exception.   The sample code below illustrates the latter fact:

 Try
   '
   ' Validating data errors and throwing an ErrorHandlingException
   '
   If c <> 0 Then
      a = b / c
   Else
      Throw New SEHComponent.ErrorHandlingException("Division", _
                         "Trying to divide by zero")
   End If
   '
   ' Trapping exceptions, other than ErrorHandlingException
   ' and re-routing it thru its class
   '
   a = b / (d * c) ' <====
   '
Catch ehe As SEHComponent.ErrorHandlingException
   ehe.Save(SEHComponent.ErrorHandlingException.TargetErrorLogs.Access, "")
Catch se As SystemException
   MessageBox.Show(se.Message, _
                   "System Exception", _
                   MessageBoxButtons.OK, _
                   MessageBoxIcon.Error)
End Try

The exception thrown by this code: a = b / (d * c) will be trapped by the SystemException instead. This is true for any Exception your code may throw.

What you should do to get these excections handled by the ErrorHandlingException is redirect the System Exceptions, as shown.

 Try
   '
   ' try block
   '
   a = b / (d * c) ' <====
   '
Catch se As SystemException
   Try
      Throw New SEHComponent.ErrorHandlingException("Re-routing it", _
                             se.Message, _
                             se)
   Catch ehe As SEHComponent.ErrorHandlingException
      ShowError(ehe)
   End Try
End Try

 

 

Sponsored Links