Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    A C# program with an exception handling attempt - now successful.Lecture 9 - slide 21 : 30
Program 3
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){
      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
    catch (IndexOutOfRangeException){                         
      int newIdx = AdjustIndex(idx,0,5);                      
      Console.WriteLine("We get element number {0}: {1}", 
                         newIdx, table[newIdx]);            
    catch (Exception){                                        
      Console.WriteLine("We cannot deal with the problem");   
      throw;     // rethrowing the exception


  public static int AdjustIndex(int i, int low, int high){
    int res;
    if (i < low)
      res = low;
    else if (i > high)
      res = high;
    else res = i;

    return res;
In a try-catch statment ...
... we handle four different
types of errors. 
Notice the rethrowing!
This catch clauses will be 
The most general catch clause.
Must be the last one.