User-Defined Exception Classes : Custom Exception : Language Basics C# Examples


C# Examples » Language Basics » Custom Exception »

 

User-Defined Exception Classes









    
using  System;

public  class  CountIsZeroException:  ApplicationException{
        public  CountIsZeroException(){
        }
        public  CountIsZeroException(string  message)  :  base(message)
        {
        }
        public  CountIsZeroException(string  message,  Exception  inner)  :  base(message,  inner)
        {
        }
}

class  MainClass{
        public  static  void  Main()  {
                try  {
                        DoAverage();
                }
                catch  (CountIsZeroException  e)
                {
                        Console.WriteLine("CountIsZeroException:  {0}",  e);
                }
        }
        public  static  void  DoAverage()  {
                throw(new  CountIsZeroException("Zero  count  in  DoAverage"));
        }

}
    
   
  
   



Output

CountIsZeroException: CountIsZeroException: Zero count in DoAverage
   at MainClass.DoAverage()
   at MainClass.Main()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Custom Exception