CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine all parts into a working calculator.

Error handling

In the final lesson we'll add error handling to the calculator. We expect two kinds of errors:

  • FormatException: thrown by double.Parse if the user enters letters instead of a number.
  • DivideByZeroException: thrown by the method if the divisor is zero.

To catch these errors we put the risky code inside try and an error message inside catch. The program then shows a clear message instead of crashing.

Project complete

Congratulations! You've completed the five-lesson calculator project. Along the way you learned to:

  • design a Calculator class with class
  • read input with Console.ReadLine and double.Parse
  • write arithmetic methods and throw DivideByZeroException
  • build a menu loop with switch and while
  • handle errors with try/catch

These patterns are used the same way in real-world applications.

Practice Add error handling with try/catch

Wrap the loop body in a try block. Add a default branch to the switch that throws new FormatException("Invalid operation"); for unknown operators. Add two catch blocks: catch (DivideByZeroException) prints Console.WriteLine("Division by zero error"); and catch (FormatException) prints Console.WriteLine("Invalid input");. The program now catches errors without crashing.

Starter code

Program.cs
using System;

class Calculator
{
    public double Add(double a, double b) { return a + b; }
    public double Subtract(double a, double b) { return a - b; }
    public double Multiply(double a, double b) { return a * b; }
    public double Divide(double a, double b)
    {
        if (b == 0) throw new DivideByZeroException("Division by zero");
        return a / b;
    }
}

class Program
{
    static void Main()
    {
        var calc = new Calculator();
        // Test cases: (operator, a, b) — some are invalid
        string[][] tests = {
            new string[] { "+", "20", "5" },
            new string[] { "/", "20", "0" },
            new string[] { "?", "20", "5" }
        };
        int i = 0;
        while (i < tests.Length)
        {
            // Add error handling with try/catch
            string op = tests[i][0];
            double a = double.Parse(tests[i][1]);
            double b = double.Parse(tests[i][2]);
            double result = 0;
            switch (op)
            {
                case "+":
                    result = calc.Add(a, b);
                    break;
                case "-":
                    result = calc.Subtract(a, b);
                    break;
                case "*":
                    result = calc.Multiply(a, b);
                    break;
                case "/":
                    result = calc.Divide(a, b);
                    break;
            }
            Console.WriteLine(result);
            i++;
        }
    }
}

Sample solution

Program.cs
using System;

class Calculator
{
    public double Add(double a, double b) { return a + b; }
    public double Subtract(double a, double b) { return a - b; }
    public double Multiply(double a, double b) { return a * b; }
    public double Divide(double a, double b)
    {
        if (b == 0) throw new DivideByZeroException("Division by zero");
        return a / b;
    }
}

class Program
{
    static void Main()
    {
        var calc = new Calculator();
        // Test cases: (operator, a, b) — some are invalid
        string[][] tests = {
            new string[] { "+", "20", "5" },
            new string[] { "/", "20", "0" },
            new string[] { "?", "20", "5" }
        };
        int i = 0;
        while (i < tests.Length)
        {
            try
            {
                string op = tests[i][0];
                double a = double.Parse(tests[i][1]);
                double b = double.Parse(tests[i][2]);
                double result = 0;
                switch (op)
                {
                    case "+":
                        result = calc.Add(a, b);
                        break;
                    case "-":
                        result = calc.Subtract(a, b);
                        break;
                    case "*":
                        result = calc.Multiply(a, b);
                        break;
                    case "/":
                        result = calc.Divide(a, b);
                        break;
                    default:
                        throw new FormatException("Invalid operation");
                }
                Console.WriteLine(result);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("Division by zero error");
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid input");
            }
            i++;
        }
    }
}

Expected output

25
Division by zero error
Invalid input

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which exception is thrown when an invalid number is entered?

  • DivideByZeroException
  • FormatException Correct answer
  • IndexOutOfRangeException
  • InvalidOperationException

Explanation: double.Parse throws FormatException on invalid format.

2 Match each exception or construct to its meaning

Term Meaning
FormatException Invalid number format
DivideByZeroException Division by zero
try Protected code block
catch Catching the error

3 Start the catch block

Question
try {
    result = calc.Divide(a, b);
} ____ (DivideByZeroException) {
    Console.WriteLine("Division by zero error");
}
Answer
try {
    result = calc.Divide(a, b);
} catch (DivideByZeroException) {
    Console.WriteLine("Division by zero error");
}

Explanation: the catch block catches the exception.

All lessons in Beginner