Exception Handling Programming Exercises C#

practicenote-01
In this Section you will learn:

  • How to solve problems if exception generates?
  • Solve practice question
Qu 1. This program is throwing exception IndexOutOfRangeException. Using your skills fix this problem using try catch block.
Answer




  • Qu 2. The given program is throwing OverflowException. Fix it.
    Answer

    Summary

    In this chapter you have did some exception handling exercises. In the next chapter we will learn constructors and Destructors in C#.


    2 thoughts on “Exception Handling Programming Exercises C#”


    1. using System;
      //using System.Collections.Generic;
      //using System.Linq;
      //using System.Text;

      namespace IndexOutOfrangeException_exception
      {
      class Program
      {
      static void Main(string[] args)
      {
      string[] list = new string[5];
      list[0] = "Sunday0";
      list[1] = "Monday1";
      list[2] = "Tuesday2";
      list[3] = "Wednesday3";
      list[4] = "Thursday4";

      for (int i = 0; i <= 5; i++)
      {
      try
      {
      if (i < 5)
      {
      Console.WriteLine(list[i].ToString());
      }
      else
      {
      throw (new IndexOutOfRangeException("\n\nException ...!\n\n"));
      }
      }

      catch (IndexOutOfRangeException ioex)
      {
      Console.WriteLine(ioex.Message.ToString());
      Console.ReadLine();
      }
      }
      }
      }

      public class IndexOutOfRangeException : Exception
      {
      public IndexOutOfRangeException(string message)
      : base(message)
      {
      }
      }
      }

    Leave a Reply

    Your email address will not be published. Required fields are marked *