Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Illustration of try-catch-finally.Lecture 9 - slide 24 : 30
Program 1
using System;

class FinallyDemo{

  internal enum Control {Returning, Jumping, Continuing, Breaking,   
                         Throwing, Normal}                           

  public static void M(Control reason){
    for(int i = 1; i <= 1; i++)  // a single iteration               
      try{                                                           
        Console.WriteLine("\nEnter try: {0}", reason);
        if (reason == Control.Returning) return;
        else if (reason == Control.Jumping) goto finish;
        else if (reason == Control.Continuing) continue;
        else if (reason == Control.Breaking) break;
        else if (reason == Control.Throwing) throw new Exception();
        Console.WriteLine("Inside try");        
      }
      catch(Exception){                                              
        Console.WriteLine("Inside catch");                           
      }  
      finally{                                                       
        Console.WriteLine("Inside finally");                         
      }
    finish: return;
  }

  public static void Main(){
    for(int i = 0; i <= 5; i++)                                      
      M((Control)i);                                                 
  }
}
 
 
 
 
Enumerating different cases
in M, shown below.
 
 
A dummy for-loop, for the sake of
the break and continue cases.
 
 
 
 
 
 
 
 
The exception handler for the
case Control.Throwing.
 
The finally clause. Executed in 
all six cases.
 
 
 
 
 
Six calls of M, for each of the cases
enumerated by the type Control.