Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- /// <summary>
- /// Divides given arguments.
- /// </summary>
- /// <returns> divident / divisor </returns>
- /// <exception cref="ArgumentException">divisor is 0</exception>
- /// <exception cref="DividentIsNegativeException">divident is negative</exception>
- /// <exception cref="ArgumentsAreZeroException">divident and divisor are 0</exception>
- public static float Divide(float divident, float divisor)
- {
- if (divident == 0 && divisor == 0) throw new ArgumentsAreZeroException();
- if (divident < 0) throw new DividentIsNegativeException(divident);
- if (divisor == 0) throw new ArgumentException();
- return divident / divisor;
- }
- static void Main(string[] args)
- {
- float a = 4, b = 2;
- try
- {
- Console.WriteLine($"{a}/{b}={Divide(a, b)}");
- }
- catch (ArgumentsAreZeroException)
- {
- Console.WriteLine("Divident and Divisor are 0");
- }
- catch (DividentIsNegativeException x)
- {
- Console.WriteLine($"Divident { x.Divident} is negative");
- }
- catch (ArgumentException)
- {
- Console.WriteLine("Divisor can't be 0");
- }
- }
- }
- class ArgumentsAreZeroException : ArgumentException
- {
- }
- class DividentIsNegativeException : ArgumentException
- {
- public float Divident { get; private set; }
- public DividentIsNegativeException(float divident) => this.Divident = divident;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement