Search This Blog

Monday, August 9, 2010

Program to implement exception handling in c#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
public class myexception : ApplicationException
{
public myexception(string message) : base(message) { }
}
class Program
{
static void Main(string[] args)
{
char ch;
ticket t = new ticket();
do
{
try
{
t.check();

}

catch (myexception me)
{
Console.WriteLine("Negative value Exception: {0}", me.Message);
}
Console.WriteLine("Do u wnt to book more tickets?(y/n): ");
ch = Convert.ToChar(Console.ReadLine());
} while (ch == 'y' || ch == 'Y');
}
}
class ticket
{

int tic = 10,a;
public void check()
{
Console.Write("enter no. of tickets u want to book: ");
a = Convert.ToInt32(Console.ReadLine());
tic = tic - a;
if (tic < 1)
{
throw (new myexception("Sorry! You can book upto 10 tickets in a day."));
}
else
{
Console.WriteLine("Remaining tickets:{0}", tic);

}

}
}
}

No comments:

Post a Comment