1. using System;
  2. using System.IO;
  3.  
  4. class Test
  5. {
  6.     public static void Main()
  7.     {
  8.         try
  9.         {
  10.             Division div = new Division();
  11.         }
  12.         catch (Exception e)
  13.         {    // alle Exceptions mit beliebiger Verschachtelungstiefe ausgeben
  14.             Exception currentEx = e;
  15.             while (currentEx != null)
  16.             {
  17.                 Console.WriteLine(currentEx.Message);
  18.                 currentEx = currentEx.InnerException;
  19.             }
  20.         }
  21.         Console.ReadKey();
  22.     }
  23. }
  24.  
  25. class Division
  26. {
  27.     static int zero = 0;
  28.     public Division()
  29.     {
  30.         try
  31.         {
  32.             int j = 1 / zero;
  33.         }
  34.         catch (DivideByZeroException dbze)
  35.         {
  36.             try
  37.             {   // Exception handling schlägt fehl (Doppelpunkt im Namen)!
  38.                 using (StreamWriter sw = new StreamWriter("ungültig:txt"))
  39.                 {
  40.                     sw.Write(dbze);
  41.                 }
  42.                 /* entspricht exaxt:
  43.                 StreamWriter sw = null;
  44.                 try
  45.                 {
  46.                     sw = sw = new StreamWriter("ungültig:txt");
  47.                     sw.Write(sw);
  48.                 }
  49.                 finally
  50.                 {
  51.                     if (sw != null)
  52.                         sw.Dispose();
  53.                 }
  54.                 */
  55.             }
  56.             catch (Exception e)
  57.             {   // Exception erneut werfen mit Parameter innerException
  58.                 throw new DivideByZeroException(dbze.Message, e);
  59.             }
  60.         }
  61.     }
  62. }
Last modified: Wednesday, 30 October 2019, 8:59 AM