C# Program: User input division with exception handling
C# Sharp Exception Handling: Exercise-1 with Solution
Write a C# program that prompts the user to input two numbers and divides them. Handle an exception when the user enters non-numeric values.
Sample Solution:
C# Sharp Code:
using System;
class Program {
static void Main() {
try {
// Prompt user for the first number and read input
Console.Write("Input the first number: ");
string input1 = Console.ReadLine();
double number1 = Convert.ToDouble(input1);
// Prompt user for the second number and read input
Console.Write("Input the second number: ");
string input2 = Console.ReadLine();
double number2 = Convert.ToDouble(input2);
// Check if the second number is not zero to avoid division by zero
if (number2 != 0) {
// Calculate and display the result of division
double result = number1 / number2;
Console.WriteLine("Result: " + result);
} else {
// Display error message if attempting to divide by zero
Console.WriteLine("Error: Cannot divide by zero.");
}
} catch (FormatException) {
// Catch block for handling non-numeric input errors
Console.WriteLine("Error: Non-numeric value entered.");
} catch (Exception ex) {
// Catch block for handling other types of exceptions
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Sample Output:
Input the first number: 1 Input the second number: 2 Result: 0.5
Input the first number: a Error: Non-numeric value entered.
Input the first number: 5 Input the second number: 0 Error: Cannot divide by zero.
Input the first number: 0 Input the second number: 12 Result: 0
Explanation:
In the above exercise,
- Inside the Main method, there is a try block that encompasses the code where potential exceptions might occur.
- The program prompts the user to input the first number through Console.Write() and reads the input as a string using Console.ReadLine(). The input is then converted to a double using Convert.ToDouble() and stored in number1.
- Similarly, the program prompts the user to input the second number. It reads the input as a string, converts it to a double, and stores it in the variable number2.
- After the inputs are obtained, number2 is checked to see if number2 is not equal to zero. If it's not zero, the program performs the division of number1 / number2 and stores the result in the variable result.
- If number2 is zero, the program executes the else block. This displays the error message "Error: Cannot divide by zero." using Console.WriteLine().
- If any FormatException exception occurs, it means the user has entered a non-numeric value. In this case, the program catches the exception and displays the error message "Error: Non-numeric value entered.".
- If any other exception occurs, the catch block of type Exception is executed, which catches all other types of exception. It retrieves the exception message using ex.Message and displays the error message "An error occurred: " followed by the exception message.
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: C# Sharp Exception Handling Exercises.
Next: Negative number exception handling.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics