0

I have 2 try and catch but after the first catch

do
{
    Console.Write("x = ");
    string str = Console.ReadLine();
    if (str.ToLower().Equals("exit"))
    {
        Console.WriteLine("Program has stopped");
        break;
    }
    else
    {
        try
        {
            x = Convert.ToInt32(str);
            Console.Write("y = ");
            y = Convert.ToInt32(Console.ReadLine());
        }
        catch (FormatException)
        {
            Console.WriteLine("Input Int or type 'exit' to stop program");
        }                       
    }

if it catches then it moves on to the rest of the code. how do I return to Input another value again after catch

try
{
    Console.WriteLine("Do you want to Add, Subtract, Multiply, or Divide ?");
    f = Console.ReadLine();
}
catch (ArgumentException)
{
    Console.WriteLine("Cannot read string");
}

if (f.ToLower().Equals("add"))
{
    sum = x + y;
    Console.WriteLine("Result: {0}", sum);
               
    while (true);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Newbie
  • 19
  • 4
  • 1
    Do not use exceptions for flow control. Use [`int.TryParse`](https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netcore-3.1) instead of Convert. – Fildor Oct 12 '20 at 15:52
  • 3
    Simply, use Int32.TryParse and get rid of the exception handler. – Steve Oct 12 '20 at 15:52
  • 3
    Use `int.TryParse` instead of `Convert.ToInt32`, so you can validate the input yourself rather than catching an exception. – Rufus L Oct 12 '20 at 15:52
  • Also, the second example is highly unlikely. An `ArgumentException` (specifically an `ArgumentOutOfRangeException`) is only thrown by `Console.ReadLine` if the input has more than `2147483647` characters. – Rufus L Oct 12 '20 at 15:56
  • _"how do I return to Input another value again after catch"_ -- to return to a place the code has already been, you need a _looping_ statement, such as `while`. See duplicates for examples. You may find the code is easier to write and read if you put the portion handling input of a number into a separate method, which you can reuse for any inputs. – Peter Duniho Oct 13 '20 at 02:41

1 Answers1

0

Although you shouldn't use exceptions for flow control, and TryParse is a much better way to handle the flow, to answer your question:

You could use a while loop for every input and if the input is validated set its condition to false. Like:

bool firstTime = true, noValidInput = true;
while(noValidInput)
{
  Console.Write(firstTime ? "x = " : "Please input Int or type 'exit' to stop program\n x = ");
  firstTime = false;
  string str = Console.ReadLine();
  if (str.ToLower().Equals("exit") || int.TryParse(str))
    noValidInput = false;
}
if (str.ToLower().Equals("exit") ...etc.

another way is make a method to get the input:

private string GetUserInput(string message, string errorMessage)
{
  Console.WriteLine(message)
  var input = Console.ReadLine();
  if (!input.ToLower().Equals("exit") && !input.TryParse(str))
    input = GetUserInput(errorMessage + message, "");
  return input;
}