C# how to handle exceptions to illustrate try-catch action with Exceptions

C# how to handle exceptions to illustrate try-catch action with Exceptions

C# Program how to handle exceptions:

Object: Write a C sharp program to illustrate try-catch action with the following Exceptions. C# Projects with Source Code

i. IndexOutOfRangeException
ii. ArgumentOutOfRangeException
iii. ArrayTypeMismatchException

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exc

{

    class exp1

    {

        public void pro1()

        {

            string[] str = new string[4];

 

            str[0] = “Let us C”;

            str[1] = “Basic C++”;

            str[2] = “C# book for beginners”;

            str[3] = “Python”;

            try

            {

                Console.WriteLine(“\n—IndexOutOfRangeException—\n”);

                Console.WriteLine(“Enter Book Name. “);

                string nam = Console.ReadLine();

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

                {

                    if (nam == str[i])

                    {

                        Console.WriteLine(“\nBook is Available in Library”);

                        break;

                    }

                }

            }

            catch (IndexOutOfRangeException e)

            {

                Console.WriteLine(“\nThe book is Already issued!!! ” + e.Message);

            }

            finally

            {

                Console.WriteLine(“\n—ArgumentOutOfRangeException—\n”);

            }

        }

    }

    class exp2

    {

        public void pro2()

        {

            int num;

            Console.WriteLine(“Enter Book Number.”);

            num = Convert.ToInt32(Console.ReadLine());

            try

            {

                if (num > 10)

                {

                    throw new ArgumentOutOfRangeException();

                }

                else

                {

                    Console.WriteLine(“Book Number is :” + num);

                }

            }

            catch (ArgumentOutOfRangeException e2)

            {

                Console.WriteLine(“Error!! ” + e2.Message);

            }

            finally

            {

                Console.WriteLine(“\n—ArrayTypeMismatchException—\n”);

            }

        }

    }

    class exp3

    {

        public void pro3()

        {

            string[] str = { “Book 1”, “Book 2″,”Book 3” };

            object[] ob = (object[])str;

            try

            {

                object obj = (object)15;

                ob[2] = obj;

            }

            catch (ArrayTypeMismatchException e3)

            {

                Console.WriteLine(“\nError!!! ” + e3.Message);

            }

            finally

            {

                Console.WriteLine(“\nLearn Programming with codeboks.com”);

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            exp1 e1 = new exp1();

            e1.pro1();

            exp2 e2 = new exp2();

            e2.pro2();

            exp3 e3 = new exp3();

            e3.pro3();

            Console.ReadKey();

        }

    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exc
{
    class exp1
    {
        public void pro1()
        {
            string[] str = new string[4];

            str[0] = "Let us C";
            str[1] = "Basic C++";
            str[2] = "C# book for beginners";
            str[3] = "Python";
            try
            {
                Console.WriteLine("\n---IndexOutOfRangeException---\n");
                Console.WriteLine("Enter Book Name. ");
                string nam = Console.ReadLine();
                for (int i = 0; i < 6; i++)
                {
                    if (nam == str[i])
                    {
                        Console.WriteLine("\nBook is Available in Library");
                        break;
                    }
                }

            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("\nThe book is Already issued!!! " + e.Message);
            }
            finally
            {
                Console.WriteLine("\n---ArgumentOutOfRangeException---\n");
            }
        }
    }
    class exp2
    {
        public void pro2()
        {
            int num;
            Console.WriteLine("Enter Book Number.");
            num = Convert.ToInt32(Console.ReadLine());
            try
            {
                if (num > 10)
                {
                    throw new ArgumentOutOfRangeException();
                }
                else
                {
                    Console.WriteLine("Book Number is :" + num);
                }
            }
            catch (ArgumentOutOfRangeException e2)
            {
                Console.WriteLine("Error!! " + e2.Message);
            }
            finally
            {
                Console.WriteLine("\n---ArrayTypeMismatchException---\n");
            }
        }
    }
    class exp3
    {
        public void pro3()
        {
            string [] str = { "Book 1", "Book 2","Book 3" };
            object [] ob = (object [])str;

            try
            {
                object obj = (object)15;
                ob[2] = obj;
            }
            catch (ArrayTypeMismatchException e3)
            {
                Console.WriteLine("\nError!!! " + e3.Message);
            }
            finally
            {
                Console.WriteLine("\nLearn Programming with codeboks.com");
            }
 
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            exp1 e1 = new exp1();
            e1.pro1();
            exp2 e2 = new exp2();
            e2.pro2();
            exp3 e3 = new exp3();
            e3.pro3();
            Console.ReadKey();
        }
    }
}

Output:

C# how to handle exceptions to illustrate try-catch action with Exceptions
C# how to handle exceptions to illustrate try-catch action with Exceptions

C# Programs Sample Code:

No.C# Projects with source code
1Implement bubble sort in C# the value of array given by the users
2Write a C# program of throwing an exception when dividing by zero condition occurs
3Create a Simple Calculator in C# Windows form Application with Source Code
4C# windows form application projects with source code
5C# Program that read array display the smallest integer and sort the array in ascending order by using Array.Sort method.
6Write a C# program to demonstrate the multiple inheritances
7C# Program Create an inheritance hierarchy that a bank might use to represent the customer’s bank accounts.
8C# program that defines a class to represent a bank account which includes the following member
9Develop a C# app that will determine the gross pay for each of three employees
10Write a C# program that reads two arrays and checks whether they are equal.
11Write a C# program to create a static function factorial and calculate factorial of the number.
12C# program to count vowels in a string
13C# Program Create a class student with a data members name and calculate marks and percentage.
14C# program which checks whether the number you inputted is an Even or Odd number
15How to Input any Number in C#

Hi, I am M.A Siddiqui. I'm a Computer Science Student, In this website, I'm teaching you Multiple languages and provides Projects and other Subjects Notes.

You may also like...

1 Response

  1. January 31, 2021

    […] C# how to handle exceptions to illustrate try-catch action with Exceptions […]

Leave a Reply

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