Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Rethrowing an exception.Lecture 9 - slide 25 : 30
Program 1
using System;

class ExceptionDemo{    
                        
  public static void Main(){
    Console.WriteLine("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("M(table,{0})", idx);   
    N(table,idx);
  }

  public static void N(int[] table, int idx){     
    Console.WriteLine("N(table,{0})", idx);       
    try{
      P(table,idx);                          
    }
    catch (IndexOutOfRangeException e){           
      // Will not/cannot handle exception here.   
      // Rethrow original exception.              
      throw;                                      
    }                                             
  }                                               

  public static void P(int[] table, int idx){
    Console.WriteLine("P(table,{0})", idx);
    Console.WriteLine("Accessing element {0}: {1}", 
                       idx, table[idx]);          
  }
}
 
 
Illustrates the idea of rethrowing 
an exception.
 
 
 
 
Main calls M, ...
 
 
 
M calls N, ...
 
 
 
N calls P in a try-catch.
 
 
 
 
On the the way back over the call
chain we catch the exception, without
really handling it. 
throw;  RETHROWS THE EXCEPTION.
With this, possible handlers in M and Main
will be able to handle the error.
 
 
 
 
The exception is raised here.