In C# all Custom Exception should be inherited from a class called as ApplicationException.
In the below sample i created a Custom Exception class called CustSalException. I am raising an Exception when some creates an object of Salary class by passing salary value less than 5000.
Please see the below code for the sample. In the code we are also logging the Error message when ever an exception is encoutered into the error log file.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int a = 3000;
try
{
Salary ss = new Salary(a);
}
catch (CustSalException cse)
{
File.WriteAllText(@”C:\error.txt”, “Error Message:”+cse.msg);
}
}
}
public class Salary
{
private int _salary;
public Salary(int sal)
{
if (_salary < 5000)
{
CustSalException ee = new CustSalException();
ee.msg = “Salary Should be more than 5000rs”;
throw ee;
}
else
_salary = sal;
}
}
public class CustSalException : ApplicationException
{
public string msg;
}












