Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class Program
  2. {
  3. /// <summary>
  4. /// Divides given arguments.
  5. /// </summary>
  6. /// <returns> divident / divisor </returns>
  7. /// <exception cref="ArgumentException">divisor is 0</exception>
  8. /// <exception cref="DividentIsNegativeException">divident is negative</exception>
  9. /// <exception cref="ArgumentsAreZeroException">divident and divisor are 0</exception>
  10. public static float Divide(float divident, float divisor)
  11. {
  12. if (divident == 0 && divisor == 0) throw new ArgumentsAreZeroException();
  13. if (divident < 0) throw new DividentIsNegativeException(divident);
  14. if (divisor == 0) throw new ArgumentException();
  15. return divident / divisor;
  16. }
  17.  
  18. static void Main(string[] args)
  19. {
  20. float a = 4, b = 2;
  21. try
  22. {
  23. Console.WriteLine($"{a}/{b}={Divide(a, b)}");
  24. }
  25. catch (ArgumentsAreZeroException)
  26. {
  27. Console.WriteLine("Divident and Divisor are 0");
  28. }
  29. catch (DividentIsNegativeException x)
  30. {
  31. Console.WriteLine($"Divident { x.Divident} is negative");
  32. }
  33. catch (ArgumentException)
  34. {
  35. Console.WriteLine("Divisor can't be 0");
  36. }
  37. }
  38. }
  39.  
  40. class ArgumentsAreZeroException : ArgumentException
  41. {
  42. }
  43.  
  44. class DividentIsNegativeException : ArgumentException
  45. {
  46. public float Divident { get; private set; }
  47. public DividentIsNegativeException(float divident) => this.Divident = divident;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement