Throwing Your Own Exceptions : Exception Throw : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Exception Throw »

 

Throwing Your Own Exceptions








    
 


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();
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Exception Throw