C# Program to Demonstrate Multiple Exceptions

This is a C# Program to demonstrate multiple exceptions.

Problem Description

This C# Program Demonstrates Multiple Exceptions.

Problem Solution

Here exceptions in C# provide a structured, uniform, and type-safe way of handling both system-level and application-level error conditions.

Program/Source Code

Here is source code of the C# Program to Demonstrate Multiple Exceptions. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate Multiple Exceptions
 */
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;
    }
}
Program Explanation

In this C# program, we are reading the first number using ‘num1’ variable and an operator using ‘op’ variable. If condition statement is used to check the value of operator ‘op’ variable is not equal to arithmetic operator ((+, -, * or /) using logical AND Operator if the condition is true then execute the statement.

advertisement
advertisement

Then we are reading the second number using ‘Num2’ variable. If condition statement is used to check that the value of ‘op’ variable is equal to ‘/’, if the condition is true then executes the statement. Using ‘Result’ variable perform the Calculator() function by passing the value ‘Num1’, ‘Num2’ and ‘op’ variables as an argument.

In Calculator() function perform the arithmetic operations using switch case statement. Here Exceptions in C# provide a structured, uniform, and type-safe way of handling both system-level and application-level error conditions. Using try and catch, an error message is displayed when the error occurs.

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.