In C# how do I define my own Exceptions?
You can create custom exception by using "Exception" class as base class
public class TestCustomException: Exception
{
public TestCustomException(string message, Exception inner)
: base(message, inner)
{
}
}
Complete Console Example
class TestCustomException : Exception
{
public TestCustomException(string message) : base(message)
{
this.HelpLink = "Sample Link details related to error";
this.Source = "This is source of Error";
}
}
class MyClass
{
public static void Show()
{
throw new TestCustomException("This is Custom Exception example in C#");
}
}
class Program
{
static void Main(string[] args)
{
try
{
MyClass.Show();
}
catch (TestCustomException ex)
{
Console.WriteLine("Error Message:-" + ex.Message);
Console.WriteLine("Hyper Link :-" + ex.HelpLink);
Console.WriteLine("Source :- " + ex.Source);
Console.ReadKey();
}
}
}
Source : Creating C# Custom Exception (With Console application example)