Shreya

Shreya

  • NA
  • 2
  • 3k

inheritance

Apr 24 2013 11:25 AM

1)      What should i do next?

      Use Inheritance to create an exception base class and various exception –derived classes.Write a program to demonstrate that the catch specifying the base class catches derived –class exceptions.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

    class Excep : System.ApplicationException

    {

        public int num1, num2;

        public Excep(int a, int b)

        {

            num1 = a;

            num2 = b;

            if (num1 == 0 || num2 == 0)

                throw new Child1();

            else if (num1 < 0 || num2 < 0)

                throw new Child2();

        }

    }

 

    public class Child1 : Excep

    {

      

    }

 

    public class Child2 : Excep

    {

 

    }

 

    class MainClass

    {

        public static void Main()

        {

        try

        {

             Excep e1= new Excep(2,0);

           

        }

        catch (Excep)

        {

            Console.WriteLine("Caught Child 1");

        }

 

        try

        {

            Excep e2 = new Excep(2, -1);

        }

        catch (Excep)

        {

 

            Console.WriteLine("Caught Child 2");

        }

    }

}

class Child1 : System.Exception

{

}

class Child2 : System.Exception

{

}


Answers (2)