Throwing Your Own Exceptions

image_pdfimage_print
   
 


using System;
   
class MyException : ApplicationException
{
    public MyException() : base("This is my exception message.")
    {
    }
}
   
class MainClass
{
    public static void Main()
    {
        try
        {
            MainClass MyObject = new MainClass();
   
            MyObject.ThrowException();
        }
        catch(MyException CaughtException)
        {
            Console.WriteLine(CaughtException.Message);
        }
    }
   
    public void ThrowException()
    {
        throw new MyException();
    }
}