Example:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomException
{
public class MyCustomException :
System.Exception
{
public MyCustomException(string txt)
: base(txt)
{
}
}
class CustomExceptionExample
{
public void DisplayAll()
{
try
{
Console.WriteLine(" Application started");
double a = 1000;
double b = 0;
Console.WriteLine("{0}/{2}",a,b,DivideVaues(a,b) );
Console.WriteLine("Error is handled in a try catch block and customised message
shown above.");
}
catch(System.DivideByZeroException e)
{
Console.WriteLine("\nDivideByZeroException Msg:{0}",e.Message);
}
catch(MyCustomException e)
{
Console.WriteLine("\nMyCustomException Msg:{0}",e.Message);
}
finally
{
Console.WriteLine("Application executed");
}
}
public double DivideVaues(double a, double b)
{
if (b == 0)
{
MyCustomException e = new MyCustomException("cannot be divided by zero,result is
infinity.");
Console.WriteLine(e);
}
if (a == 0)
{
MyCustomException e = new MyCustomException("cannot have a zeroDivisor");
Console.WriteLine(e);
}
return a / b;
}
static void Main(string[] args)
{
Console.WriteLine("Example of customizes exception devloped in C#.");
CustomExceptionExample cee = new CustomExceptionExample();
cee.DisplayAll();
}
}
}
Output of the program:
Thank you.
If you have any suggestions and comment about by blogs, please let me know.

EhteshamPosted Sep 24, 2012, 1:41 PM
for a note custom exception classes should always have Public access modifier