Как отловить ошибки в Catch
как сделать чтоб прописать catch без tryparse строчек что вводится число чтоб если не вводится выкидывался catch до метода
если кратко то мне нужно убрать tryparse and if == 0 и чтоб работал на них сразу catch если я их уберу то catch отрабатывает только после того как я введу X and Y
namespace project_1;
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine("Enter operation divider = 1 , exit = 2");
var choose = Console.ReadLine()!;
switch (choose)
{
case "1" :
try
{
Console.WriteLine("Enter X num");
var x = Console.ReadLine();
if (x == "0")
{
Console.WriteLine("Вы делите на ноль повторите действие");
break;
}
if (int.TryParse(x , out var number))
{
Console.WriteLine("Enter Y num");
var y = Console.ReadLine();
var result =Divider.Divide(x, y);
Console.WriteLine($"result divide = {result}");
}
else
{
Console.WriteLine($"вы ввели не чилсо повторите заново действие из списка ");
}
}
catch (DivideByZeroException)
{
Console.WriteLine("Division by 0 is forbidden!\n");
}
catch (FormatException)
{
Console.WriteLine("This is NOT a number Wrong!!!\n");
}
break;
case "2" :
Console.WriteLine("you have logged out of the operation, re-enter if you want to reuse ");
return;
default: Console.WriteLine("this operation is not on the list choose from the list");
break;
}
}
}
}
using System.Diagnostics;
namespace project_1;
public class Divider
{
public static int Divide(string x, string y)
{
var firstNum = Convert.ToInt32(x);
var secondNum = Convert.ToInt32(y);
var result = firstNum / secondNum;
return result;
}
}
на фото я показываю как работает без tryparse and if

должно быть вот так только прописать эту логику чтоб их ловил catch
