Exception Handling

 using System;

using System.IO;

using System.Linq;

using System.Collections.Generic;


namespace CSharp_Shell

{


 public class Program

 {

  public static void Main()

  {

   /*

   //NullReferenceException

   string text = null;

   try

   {

    int length = text.Length;

    Console.WriteLine(length);

   }

   catch (NullReferenceException ex)

   {

    Console.WriteLine(ex.Message);

   }

   */

   /*

   //IndexOutOfRangeException

   string[] list = new string[5];

   list[0] = "Sunday";

   list[1] = "Monday";

   list[2] = "Tuesday";

   list[3] = "Wednesday";

   list[4] = "Thursday";

   //

   try

   {

    for (int i = 0; i <= 5; i++)

    {

     Console.WriteLine(list[i].ToString());

    }

   }

   catch (IndexOutOfRangeException e)

   {

    Console.WriteLine(e.Message);

   }

   */

   /*

   //OverflowException

   int num1, num2;

   byte result;

   //

   num1 = 30;

   num2 = 60;

   try

   {

    result = Convert.ToByte(num1 * num2);

    Console.WriteLine("{0} x {1} = {2}", num1, num2, result);

   }

   catch (OverflowException e)

   {

    Console.WriteLine(e.Message);

   }

   */

  }

 }

}

Comments

Popular Posts