Exceptions - C# Programming Exercise

This exercise in C# aims to develop a program that asks the user for two numbers and displays their division. The program should handle potential errors using a try..catch block to catch any exceptions, such as division by zero. If the user attempts to divide by zero, the program should display an appropriate error message, while if the entered numbers are valid, the program should display the result of the operation.

This type of exercise is useful for learning how to work with exceptions in C#, especially in cases where mathematical operations can result in errors like division by zero. The use of try..catch is essential in programming to handle errors in a controlled manner and prevent the program from crashing unexpectedly. Additionally, this exercise reinforces the importance of validating input and properly handling errors in the development of robust applications.

 Category

Flow Control

 Exercise

Exceptions

 Objective

Write a C# program to prompt the user for two numbers and display their division. Errors should be caught using "try..catch"

 Write Your C# Exercise

using System;  // Import the System namespace, which contains fundamental classes like Console

class Program  // Define the Program class
{
    static void Main()  // The entry point of the program
    {
        try  // Start the try block to catch potential errors
        {
            // Ask the user for the first number (numerator)
            Console.Write("Enter the first number (numerator): ");
            int num1 = int.Parse(Console.ReadLine());  // Read and convert the first input to an integer

            // Ask the user for the second number (denominator)
            Console.Write("Enter the second number (denominator): ");
            int num2 = int.Parse(Console.ReadLine());  // Read and convert the second input to an integer

            // Perform the division and display the result
            int result = num1 / num2;  // Divide the first number by the second
            Console.WriteLine($"The result of {num1} divided by {num2} is: {result}");  // Output the result
        }
        catch (DivideByZeroException)  // Catch the specific exception when trying to divide by zero
        {
            // Handle the divide by zero error
            Console.WriteLine("Error: Cannot divide by zero.");  // Display an error message
        }
        catch (FormatException)  // Catch the format exception if the user doesn't input valid integers
        {
            // Handle the invalid input error
            Console.WriteLine("Error: Please enter valid integers.");  // Display an error message
        }
        catch (Exception ex)  // Catch any other unexpected exceptions
        {
            // Handle any general exception
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");  // Display a general error message
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Positive and negative

    In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prom...

  •  Multiply if not zero

    In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and ...

  •  Divide if not zero

    In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...

  •  Divide if not zero (Using else)

    In this C# exercise, you will learn how to modify the previous program using the else control structure. The program will ask the user for two numbers, and if the sec...

  •  Greatest of three numbers

    In this C# exercise, you will learn how to write a program that prompts the user to enter three numbers and displays the greatest one. The program will use conditional struc...

  •  Repeat until 0

    In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep as...