SIGN UP MEMBER LOGIN:    
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.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks so much !!!

Posted by pham nghiep Dec 22, 2011

Good Work.........

Posted by Aaron Cronje Dec 10, 2011

Thanks great job.

Posted by Akshay Teotia Dec 10, 2011

hai this is very useful article...

thanku very much..for presenting 4 us...

but i have got an error while bulding the project CustomMessage class in the project.

i have bulild Enumerations project,Add another project called CustomException,build it successfully build..

now add a class file to CustomException..named as CustomMessage.

i have written the code as it was given...but i am getting an error as..

Inconsistent accessibility: parameter type 'CustomException.Exception' is less accessible than method 'CustomException.CustomException.CustomException(CustomException.SeverityLevel, CustomException.LogLevel, CustomException.Exception, string)

in the CustomException class....

 

please tell me..wat is the problem.

suneel

Posted by suneel dotnet Oct 05, 2006
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor