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

 

 

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 11/16/2006)
Rating
The article was in-depth and provided an excellent amount of information.

Chas
 
Comment #2  (Posted by an unknown user on 11/21/2006)
Rating
It was very well designed and useful to understand how I can add this to my systems. However, after moving the TargetErrorLog.mdb to c:\temp and fixing the reference to ErrorHandlingException.dll, the program will only run correctly within VB.net and not as a stand alone exe. I did build the solution and the program. The error I get when running it outside of VB.Net is: File or assembly name ErrorHandlingException, or one of its dependencies, was not found. Maybe this shows my inexperience with VB.Net?
 
Comment #3  (Posted by an unknown user on 11/21/2006)
Rating
It was very well designed and useful to understand how I can add this to my systems. However, after moving the TargetErrorLog.mdb to c:\temp and fixing the reference to ErrorHandlingException.dll, the program will only run correctly within VB.net and not as a stand alone exe. I did build the solution and the program. The error I get when running it outside of VB.Net is: File or assembly name ErrorHandlingException, or one of its dependencies, was not found. Maybe this shows my inexperience with VB.Net?
 
Comment #4  (Posted by Morten Grøtan on 10/29/2007)
Rating
The GetApplicationAttributes method can be greatly simplified by using the AssemblyName object instead of the AssemblyQualifiedName, using this (C#) code:

// C# "short-circuit" mechanism makes sure that the if-check exits at the first false expression
if ((targetSite != null) && (targetSite.ReflectedType != null) && (targetSite.ReflectedType.Assembly != null))
{
// This is the same info as in AssemblyQualifiedName, only represented as an object
AssemblyName assemblyName = targetSite.ReflectedType.Assembly.GetName();

Version version = assemblyName.Version;
if (version != null)
{
_objectVersion = version.ToString();
foundVersion = true;
}

CultureInfo culture = assemblyName.CultureInfo;
if ((culture != null) && (culture.ToString().Length > 0))
{
_objectCulture = culture.ToString();
foundCulture = true;
}
}

 
Sponsored Links