class NotEnoughStockException:Exception { public override string ToString() { return "Not Enough Stock"; } }
class SalesItem { string name; int stock; public SalesItem(string Name,int Stock) { this.name = Name; this.stock = Stock; } public void Sell(int Qty) { if (Qty > stock) throw new NotEnoughStockException(); stock -= Qty; } public void Show() { Console.WriteLine("{0} :{1}Kg",name,stock); } }
class Program { static void Main(string[] args) { try { SalesItem i = new SalesItem("Dal", 10); i.Show(); i.Sell(5); i.Show(); i.Sell(13); i.Show(); } catch (NotEnoughStockException e) { Console.WriteLine("There was an error!"); Console.WriteLine(e); } } }}