Lab-3 by 3075
Lab-3 by 3075
Experiment 3
AIM: Program on Polymorphism and Exception Handling
Code:
Polymorphism
using System;
namespace PolymorphismAndException
{
class Cartype
{
public virtual void Type()
{
Console.WriteLine("This is a generic car.");
}
class Excep
{
public void Div(int numerator, int divisor)
{
try
{
int result = numerator / divisor;
Console.WriteLine($"Result of {numerator} / {divisor} is: {result}");
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Exception caught: Division by zero is not allowed.");
Console.WriteLine("Details: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An unexpected error occurred.");
Console.WriteLine("Details: " + ex.Message);
}
}
}
class MainClass
{
static void Main(string[] args)
{
Cartype c1 = new Cartype();
CarSound s1 = new CarSound();
Excep e1 = new Excep();
switch (choice)
{
case 1:
Console.WriteLine("Executing polymorphism:");
case 2:
Console.WriteLine("Executing exception handling:");
e1.Div(10, 2);
e1.Div(10, 0);
default:
Console.WriteLine("Invalid choice. Please choose 1 or 2.");
break;
}
}
else
{
Console.WriteLine("Invalid input. Please enter a number.");
}
}
}
}
Output-