Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    A C# program with an exception handling attempt - not a success.Lecture 9 - slide 21 : 30
Program 1
using System;

class ExceptionDemo{

  public static void Main(){
    int[] table = new int[6]{10,11,12,13,14,15};
    int idx = 6;

    M(table, idx);
  }

  public static void M(int[] table, int idx){
    try{                                                     
      Console.WriteLine("Accessing element {0}: {1}", 
                         idx, table[idx]);
    } 
    catch (NullReferenceException){                          
      Console.WriteLine("A null reference exception");
      throw;      // rethrowing the exception
    }   
    catch (DivideByZeroException){                           
      Console.WriteLine("Divide by zero");
      throw;      // rethrowing the exception                
    }

  }
}
 
 
 
 
 
 
 
 
 
 
 
 
In a try-catch statement ...
 
 
 
... we handle null reference and ...
 
 
 
... divide by zero exceptions.
 
Both in vain.