ARTICLE

Custom Exception Handling in C#

Posted by Susan Abraham Articles | Exception Handling C# May 17, 2005
This article discusses the implementation of Custom Exception Handling using the existing features of C# .Net.
Reader Level:

Introduction

Enhanced Exception Handling is one of key features that is most prominently available in .Net both 2003 version and Whidbey versions of C#.

This paper discusses the implementation of Custom Exception Handling using the existing features of C# .Net.

The Concept

The whole idea of having customized Exception Handling is centered around the fact that there needs to be a generic approach of catching and throwing Exceptions.

For implementing custom Exception Handling we need to derive the class CustomException from the system base class ApplicationException. In general, for customizing Exception Handling the following components are necessary:

  1. A custom exception class which would host the exception thrown and can be thrown as a new exception.
  2. A message class that would host all user - friendly Application messages that need to be displayed.

Implementation

The following are the steps that are required to implement custom exception for creation of the above mentioned components:

Step 1:

Define a project called Enumerators that would contain the definitions of all the Enumerators as the following:

using System;
namespace
CustomException
{
///
<summary>
///
Severity level of Exception
///
</summary>
public enum
SeverityLevel
{
Fatal,
Critical,
Information
}
///
<summary>
///
Log level of Exception
///
</summary>
public enum
LogLevel
{
Debug,
Event
}
}

  1. The Severity level determines the criticality of the error.
  2. The Loglevel determines whether an entry needs to be made in Log. Based on the log level chosen , entries can be made either in the Debug Log or System Event Log .

Step 2:

Add another project named CustomException. Add a reference of the Enumerators project. To the project add the following class deriving from ApplicationException:

using System;
namespace
CustomException
{
///
<summary>
///
Summary description for CustomException
///
</summary>
public class
CustomException : ApplicationException
{
// private members
// defines the severity level of the Exception
private
SeverityLevel severityLevelOfException ;
// defines the logLevel of the Exception
private
LogLevel logLevelOfException ;
// System Exception that is thrown
private
Exception innerException ;
// Custom Message
private string
customMessage ;
///
<summary>
///
Public accessor of customMessage
///
</summary>
public string
CustomMessage
{
get {return this
.customMessage; }
set {this.customMessage = value
; }
}
///
<summary>
///
Standard default Constructor
///
</summary>
public
CustomException( )
{ }
///
<summary>
///
Constructor with parameters
///
</summary>
///
<param name="severityLevel"></param>
///
<param name="logLevel"></param>
///
<param name="exception"></param>
/// <param name="customMessage"></param>

public CustomException( SeverityLevel severityLevel , LogLevel logLevel, Exception exception, string
customMessage)
{
this
.severityLevelOfException = severityLevel ;
this
.logLevelOfException = logLevel ;
this
.innerException = exception ;
this
.customMessage = customMessage ;
}
}
}

One advantage of creating a custom Exception class is that the Constructor can be enabled to writing to a Log on instantiation of the CustomException Object using TraceListeners. The entry to the log would be based on the logLevel. This would force - write an entry each time the custom Exception is thrown.

Thus we have a customException which could be thrown in the catch - handlers of system - defined exceptions.

Step 3:

For implementing the CustomMessage component , create an Exception.resx File that would host the error string and the corresponding message string as key-value pair.

Ex. "Test", "Testing Error"

Step 4:

Add to project a CustomMessage.cs class File. This File would look in the following way:

using System.Threading;
using
System.Resources;
using
System.Reflection;
namespace
CustomException
{
///
<summary>
///
Summary description for CustomMessage.
///
</summary>
public class
CustomMessage
{
public
CustomMessage()
{
}
public string GetString(string
key)
{
// Create a resource manager to retrieve resources.
ResourceManager rm = new
ResourceManager("Exceptions", Assembly.GetExecutingAssembly());
// Get the culture of the currently executing thread.
// The value of ci will determine the culture of
// the resources that the resource manager retrieves.
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
// Retrieve the value of the string resource and return it
String str = rm.GetString(key, ci);
return
str ;
}
}
}

The GetString() of the CustomMessage class is used to retrieve the value string corresponding to the key passed as parameter from the Exceptions resource File.

Usage

a) Add to existing Solution a Test Windows Applications Project.

b) Add the References to both the CustomException and the Enumerators projects.

c) Write a function which would throw a system exception , encapsulated in a try-catch block as follows :

private void Updater()
{
try

{
int
i = 0;
int
j = 8 ;
int
k= j/i;
}
catch
(Exception ex)
{
SeverityLevel severityLevel = SeverityLevel.Critical;
LogLevel logLevel = LogLevel.Debug;
CustomMessage customMessage =
new
CustomMessage();
throw new
CustomException.CustomException( severityLevel,logLevel,ex.InnerException,customMessage.GetString("Test"));
}
}

d) In the Catch Block , re-throw the Exception as a CustomException

e) On a button_click event handler add the following code :

private void buttonTest_Click(object sender, System.EventArgs e)
{
try
{
this
.Updater();
}
catch
(CustomException.CustomException ce)
{
MessageBox.Show(ce.CustomMessage);
}
}

Conclusion

The above is one of the several methods of implementing Custom Exceptions in C#. Another method would be using Application Blocks. Yet another method would be specifically defining all the Application Exceptions as separate classes and using them.

Article Extensions
Contents added by Javier Castanos on Sep 18, 2013
Greats!
COMMENT USING

Trending up