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
{
}
VulpesPosted Apr 24, 2013, 6:59 PM
I think you're trying to do too much with your exception classes, Shreya, which are really just providing information about some error that has occurred and rarely need to do any calculations.
As the question you've been given is vague, I'd just do something minimal like the following:
The output should be:
Javeed M ShaikhPosted Apr 24, 2013, 12:31 PM