I am new to coding, I hope you can help me to improve my code :)
First of all: The code works correctly.
using System;
namespace _3_dividing_2numbers
{
class Program
{
static void Main(string[] args)
{
try
{
OperationDevide();
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Enter another number !");
OperationDevide();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
OperationDevide();
}
}
public static void OperationDevide()
{
Console.WriteLine("Enter your first number :");
var firstNum = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your second number :");
var secondNum = Convert.ToInt32(Console.ReadLine());
var result = firstNum / secondNum;
Console.WriteLine($"your result is : {result}");
}
}
}
Code description
I write a simple program that takes two int inputs and prints the result. but I want to check the possible errors so I used from try/catch for DivideByZeroException
and all of the other errors by Exception
.
My Problem
Do you have any idea to make this code better? I want to keep asking the user until the inputs have the correct format. when I run my code after the second Zero input it's crashed and try/catch only works for the first round of Zero inputs.
OperationDivide
. \$\endgroup\$