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.
Custom Exception Handling in C# - 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:
- A custom exception class which would host the exception thrown and can be thrown as a new exception.
- A message class that would host all user - friendly Application messages that need to be displayed.
Implementing Custom Exception Handling
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
}
}
- The Severity level determines the criticality of the error.
- 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 ;
}
}
}
Ajeet MishraPosted Sep 1, 2015, 6:07 AM
good one
Gowtham RajamanickamPosted Apr 18, 2015, 3:50 AM
good one
vinoth arunrajeditedPosted Apr 8, 2013, 2:41 AMEdited Apr 8, 2013, 2:41 AM
Hi,1) We can have more than one custom exception (Its grow able like more than 50) in a application, Is good?2) How can we handle custom exception like run time system exception?
Yogesh Singh RawatPosted Oct 15, 2012, 2:34 AM
Nice topic...
pham nghiepPosted Dec 22, 2011, 2:53 AM
Thanks so much !!!
Aaron CronjePosted Dec 10, 2011, 12:25 AM
Good Work.........
Akshay TeotiaPosted Dec 10, 2011, 12:23 AM
Thanks great job.
suneel dotnetPosted Oct 5, 2006, 5:20 AM
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