Catch exception with wrong type inside a function : Try Catch : Language Basics C# Examples


C# Examples » Language Basics » Try Catch »

 

Catch exception with wrong type inside a function









    
using  System;

class  MainClass
{
        
        static  void  AFunction()
        {
                
                try  {
                        int  Zero  =  0;
                        int  j  =  22  /  Zero;
                }  catch  (ArgumentOutOfRangeException  e)  //  this  exception  doesn't  match
                {
                        Console.WriteLine("OutOfRangeException:  {0}",  e);
                }
                Console.WriteLine("In  AFunction()");
        }
        public  static  void  Main()
        {
                try
                {
                        AFunction();
                }
                //  this  exception  doesn't  match
                catch  (ArgumentException  e)
                {
                        Console.WriteLine("ArgumentException  {0}",  e);
                }catch(Exception  ee){
                        Console.WriteLine("Exception  {0}",  ee);
                }
        }
}
    
   
  
   



Output

Exception System.DivideByZeroException: Attempted to divide by zero.
   at MainClass.AFunction()
   at MainClass.Main()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Try Catch