How to handle a specific exception : Try Catch : Language Basics C# Examples


C# Examples » Language Basics » Try Catch »

 

How to handle a specific exception









    
using  System;

class  MainClass{

    public  static  void  Main()  {

        try  {

            int  zero  =  0;
            Console.WriteLine("In  try  block:  attempting  division  by  zero");
            int  myInt  =  1  /  zero;

        }  catch  (DivideByZeroException  myException)  {
            Console.WriteLine("Message  =  "  +  myException.Message);
            Console.WriteLine("StackTrace  =  "  +  myException.StackTrace);
        }
    }
}
    
   
  
   



Output

In try block: attempting division by zero
Message = Attempted to divide by zero.
StackTrace =    at MainClass.Main()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Try Catch