Assignment: below is the program which calculates power of any number. If we pass a negative power number it will generate an exception.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exception_Power_demo
{
class F1
{
void pow(int x, int y)
{
if (y < 0)
throw new FormatException();
int p = 1;
while (y > 0)
{
p = p * x;
y--;
}
Console.WriteLine("power{0}", p);
}
class M
{
static void Main(string[] args)
{
F1 f = new F1();
try
{
f.pow(4,2);
f.pow(-4,-1);
f.pow(3,2);
}
catch (Exception ex)
{
Console.WriteLine("Error {0}", ex.Message);
}
Console.ReadLine();
}
}
}
}
Comments
Post a Comment