René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback
 

Exceptions in .net

using System;
using System.Collections;

class N {
  public void Print(String s) {
    Console.WriteLine(s);
  }
}

class M {

  enum do_what {
    array,
    reference,
    hashtable,
    arg_null
  };

  public static void Main() {
    M m = new M();
    m.DoAll();
  }

  private void DoAll() {
    Do(do_what.array);
    Do(do_what.reference);
    Do(do_what.hashtable);
    Do(do_what.arg_null);
  }

  private void Do(do_what d) {
     try {
       switch (d) {
          case do_what.array:     Array();     break;
          case do_what.reference: Reference(); break;
          case do_what.hashtable: HashTable(); break;
          case do_what.arg_null:  ArgNull();   break;
       }
     }
     catch (IndexOutOfRangeException e) {
       PrintExc("Out Of Range", e);
     }
     catch (NullReferenceException e) {
       PrintExc("Null Reference", e);
     }
     catch (InvalidOperationException e) {
       PrintExc("Invalid Operation", e);
     }
     catch (ArgumentNullException e) {
       PrintExc("Argument Null", e);
     }
  }

  private void Array() {
    int[] a;

    a = new int[4];

    for (int i=0;i<10;i++) {
      a[i]=i;
    }
  }

  private void Reference() {
    N n = null;
    PassN(n);
  }

  private void HashTable() {
    Hashtable ht = new Hashtable();
    ht.Add("Eins", 1);
    ht.Add("Zwei", 2);
    ht.Add("Drei", 3);
    ht.Add("Vier", 4);
    ht.Add("Fuenf",5);
    
    HashTableOut(ht);
  }

  private void HashTableOut(Hashtable ht) {
    IDictionaryEnumerator iter = ht.GetEnumerator();

    int i =  0;
    while (iter.MoveNext()) {
      Console.WriteLine("{0}\t{1}", iter.Key, iter.Value);
      if (i++ >=3) {
        ht.Remove("Drei");
      }
    }

  }

  private void PassN(N n) {
    n.Print("zweiundvierzig");
  }

  private void PrintExc(String s, Exception e) {
     Console.WriteLine("{0}: {1}", s, e.Message);
  }

  private int ArgNull() {
    String s = null;
    return "einundvierzig acht/achtel".IndexOf(s); 
  }
}