Saturday, 2 December 2017

C# Program to Demonstrate Multiple Exceptions


Code:

using System;
class Exercise
{
    static void Main()
    {
        double Num1, Num2;
        double Result = 0.00;
        char op;
        try
        {
            Console.Write("Enter your First Number :  ");
            Num1 = double.Parse(Console.ReadLine());
            Console.Write("Enter an Operator  (+, -, * or /): ");
            op = char.Parse(Console.ReadLine());
            if (op != '+' && op != '-' &&
                op != '*' && op != '/')
                throw new Exception(op.ToString());
            Console.Write("Enter your Second Number :");
            Num2 = double.Parse(Console.ReadLine());
            if (op == '/')
                if (Num2 == 0)
                    throw new DivideByZeroException("Division by zero is not allowed");
            Result = Calculator(Num1, Num2, op);
            Console.WriteLine("\n{0} {1} {2} = {3}", Num1, op, Num2, Result);
        }
        catch (FormatException)
        {
            Console.WriteLine("The number you typed is not valid");
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Operation Error: {0} is not a valid op", ex.Message);
        }
        Console.Read();
    }

    static double Calculator(double v1, double v2, char op)
    {
        double Result = 0.00;

        switch (op)
        {
        case '+':
              Result = v1 + v2;
              break;
        case '-':
              Result = v1 - v2;
              break;
        case '*':
              Result = v1 * v2;
              break;
        case '/':
              Result = v1 / v2;
              break;
        }
        return Result;
    }
}


Output:

Enter Your First Number : 10
Enter an Operator  (+, -, * or /) : ,
Operation Error : , is not a Valid Operator


More C# Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...