// Exercise 1: custom exception + Withdraw validation (easy) // // Implement: // - InsufficientFundsException: derives from Exception, constructor takes // (decimal requested, decimal available), calls base(message) with // "requested {requested}, only {available} available", and stores both // values as public get-only properties Requested/Available. // - Withdraw(balance, amount): returns balance - amount if amount <= balance // and amount > 0; throws InsufficientFundsException if amount > balance; // throws ArgumentOutOfRangeException if amount <= 0. // // Run with: dotnet run (inside a console project) or paste into a Main method. using System; class InsufficientFundsException : Exception { public decimal Requested { get; } public decimal Available { get; } public InsufficientFundsException(decimal requested, decimal available) // TODO: call base(...) with the formatted message, then set // Requested and Available. : base("") { } } class Program { #pragma warning disable IDE0060 static decimal Withdraw(decimal balance, decimal amount) { // TODO: validate amount > 0 (else ArgumentOutOfRangeException), // validate amount <= balance (else InsufficientFundsException), // otherwise return balance - amount. return 0m; } #pragma warning restore IDE0060 static void Main() { int pass = 0, total = 0; total++; if (Withdraw(100m, 40m) == 60m) { Console.WriteLine("PASS: normal withdrawal"); pass++; } else Console.WriteLine("FAIL: normal withdrawal"); total++; try { Withdraw(100m, 200m); Console.WriteLine("FAIL: expected InsufficientFundsException"); } catch (InsufficientFundsException e) { if (e.Requested == 200m && e.Available == 100m && e.Message == "requested 200, only 100 available") { Console.WriteLine("PASS: InsufficientFundsException thrown with correct data"); pass++; } else Console.WriteLine("FAIL: InsufficientFundsException data wrong, got " + e.Message); } total++; try { Withdraw(100m, -5m); Console.WriteLine("FAIL: expected ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { Console.WriteLine("PASS: ArgumentOutOfRangeException thrown for non-positive amount"); pass++; } catch (InsufficientFundsException) { Console.WriteLine("FAIL: wrong exception type for non-positive amount"); } Console.WriteLine($"--- {pass}/{total} checks passed ---"); } }