illustrates multiple catch blocks

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example13_4.cs illustrates multiple catch blocks
*/

using System;

public class Example13_4
{

  public static void Main()
  {

    try
    {

      int[] myArray = new int[2];
      Console.WriteLine("Attempting to access an invalid array element");
      myArray[2] = 1;

    }
    catch (DivideByZeroException e)
    {

      // code that handles a DivideByZeroException
      Console.WriteLine("Handling a System.DivideByZeroException object");
      Console.WriteLine("Message = " + e.Message);
      Console.WriteLine("StackTrace = " + e.StackTrace);

    }
    catch (IndexOutOfRangeException e)
    {

      // code that handles an IndexOutOfRangeException
      Console.WriteLine("Handling a System.IndexOutOfRangeException object");
      Console.WriteLine("Message = " + e.Message);
      Console.WriteLine("StackTrace = " + e.StackTrace);

    }
    catch (Exception e)
    {

      // code that handles a generic Exception: all other exceptions
      Console.WriteLine("Handling a System.Exception object");
      Console.WriteLine("Message = " + e.Message);
      Console.WriteLine("StackTrace = " + e.StackTrace);

    }

  }

}