Write a Program to show Exception Handling a.DivideByZero b.ArrayIndexOutOfBound


a. DivideByZero

Source code
a.        
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
         static void Main(string[] args)
        {
            int[] a = new int[5];
            int[] b = new int[5];
             int []c=new int [5];
             Console.WriteLine("Enter the values for array a");
            for(int i=0;i<5;i++)
            {
              
                a[i]=Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Enter the values for array b");
            for(int i=0;i<5;i++)
            {
               
                b[i] = Convert.ToInt32(Console.ReadLine());
            }
            try
            {
                for(int i=0;i<5;i++)
                {
                    c[i] = a[i] / b[i];
                    Console.WriteLine(c[i]);
                }
            }
            catch (DivideByZeroException e1)
            {
                Console.WriteLine(e1.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                Console.ReadKey();
            }


         }
    }
}

bb.   ArrayIndexOutOfBound

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

namespace ConsoleApplication5
{
    class Program
    {
         static void Main(string[] args)
        {
            int[] a = new int[5];
            int[] b = new int[5];
             int []c=new int [5];
             Console.WriteLine("Enter the values for array a");
            for(int i=0;i<5;i++)
            {
              
                a[i]=Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Enter the values for array b");
            for(int i=0;i<5;i++)
            {
               
                b[i] = Convert.ToInt32(Console.ReadLine());
            }
            try
            {
                for(int i=0;i<=5;i++)
                {
                    c[i] = a[i] / b[i];
                    Console.WriteLine(c[i]);
                }
            }
            catch (IndexOutOfRangeException e1)
            {
                Console.WriteLine(e1.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                Console.ReadKey();
            }


         }
    }
}

0 comments:

Feel free to contact the admin for any suggestions and help.