w3resource

C# Program: Divide two numbers with exception handling

C# Sharp Exception Handling: Exercise-8 with Solution

Write a C# program that implements a method that divides two numbers. Handle the DivideByZeroException that occurs if the denominator is 0.

Sample Solution:

C# Sharp Code:

using System;

class Program {
  static void Main() {
    try {
      // Prompt the user to input the numerator and convert it to an integer
      Console.Write("Input the numerator: ");
      int numerator = Convert.ToInt32(Console.ReadLine());

      // Prompt the user to input the denominator and convert it to an integer
      Console.Write("Input the denominator: ");
      int denominator = Convert.ToInt32(Console.ReadLine());

      // Call the DivideNumbers method to perform division and store the result
      int result = DivideNumbers(numerator, denominator);

      // Display the division result to the user
      Console.WriteLine("Result: " + result);
    } catch (DivideByZeroException) {
      // Catch block for handling DivideByZeroException (division by zero error)
      Console.WriteLine("Error: Division by zero is not allowed.");
    } catch (FormatException) {
      // Catch block for handling FormatException (invalid input format)
      Console.WriteLine("Error: Invalid input. Please enter valid integers.");
    } catch (Exception ex) {
      // Catch block for handling other types of exceptions
      Console.WriteLine("An error occurred: " + ex.Message);
    }
  }

  // Method to perform division of two integers
  static int DivideNumbers(int numerator, int denominator) {
    return numerator / denominator; // Perform integer division
  }
}

Sample Output:

Input the numerator: 4
Input the denominator: 3
Result: 1
 
Input the numerator: 12
Input the denominator: 0
Error: Division by zero is not allowed.
 
Input the numerator: 0
Input the denominator: 12
Result: 0

Explanation:

In the above exercise,

  • The "Main()" method prompts the user to input the numerator and denominator using Console.Write() and Console.ReadLine(). The inputs are converted to integers using Convert.ToInt32() and stored in the numerator and denominator variables, respectively.
  • The program calls the DivideNumbers method, passing the numerator and denominator as arguments.
  • The DivideNumbers method divides the numerator by the denominator. If the denominator is 0, it will throw a DivideByZeroException.
  • In the "Main()" method, exceptions are handled using catch blocks. If a DivideByZeroException occurs, it means the user entered 0 as the denominator. The program catches this exception and displays an appropriate error message.
  • If a FormatException occurs, the user entered a non-integer value. This exception is caught and an appropriate error message is displayed.
  • Any other exceptions are caught by the generic catch block, and a general error message is displayed.

Flowchart:

Flowchart: C# Sharp Exercises - Divide two numbers with exception handling.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Read and handle Int32 range exceptions in user input.
Next: Convert user input to datetime with exception handling.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/exception-handling/csharp-exception-handling-exercise-8.php