Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    A C# program with deeper exception propagation chain.Lecture 9 - slide 22 : 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;

    Console.WriteLine("Main");
    try{
      M(table, idx);                            
    }   
    catch (IndexOutOfRangeException){           
      M(table, AdjustIndex(idx,0,5));           
    }
  }

  public static void M(int[] table, int idx){
    Console.WriteLine("M(table,{0})", idx);
    N(table,idx);                               
  }

  public static void N(int[] table, int idx){
    Console.WriteLine("N(table,{0})", idx);
    P(table,idx);                                
  }

  public static void P(int[] table, int idx){            
    Console.WriteLine("P(table,{0})", idx);              
    Console.WriteLine("Accessing element {0}: {1}",      
                       idx, table[idx]);
  }



  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;
  }
}
 
 
 
 
 
 
 
 
 
 
Main calls M.
 
The handling of the error.
Retrying the call of M.
 
 
 
 
 
M calls N.
 
 
 
 
N calls P.
 
 
In P the error occurs.
The error object is propagated 
back to Main.
 
 
 
 
 
The usual method.