CodeBlanks Logo Icon CodeBlanks

Putting it together

C# course · Beginner · Lesson 23/116

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.

Exercise 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.

Show example solution
Wrap the loop body in try and add two catch blocks.
Example solution:
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++;
        }
    }
}

Test yourself

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

    • FormatException
    • DivideByZeroException
    • IndexOutOfRangeException
    • InvalidOperationException
    Show answer

    FormatException — Correct; double.Parse throws FormatException on invalid format.

  2. Match each exception or construct to its meaning

    FormatException
    Invalid number format
    DivideByZeroException
    Division by zero
    try
    Protected code block
    catch
    Catching the error
  3. Start the catch block

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

All lessons in Beginner