Using try and catch - CSharp Custom Type

CSharp examples for Custom Type:try catch finally

Introduction

The try command wraps around a block of code that helps you route any problems that might occur.

The catch keyword catches the exceptions that the try command routes.

Demo Code

using System;//www .j a  v  a2  s  .com
class TryIt
{
   public static void Main()
   {
      int [] myArray = new int[5];
      try
      {
         for ( int i = 0; i < 10; i++ )  // Array only has 5 elements!
         {
            myArray[i] = i;
         }
      }
      catch
      {
         Console.WriteLine("The exception was caught!");
      }
      Console.WriteLine("At end of class");
   }
}

Result


Related Tutorials