-2

I'm trying to restart my program after I catch an error using the catch() function, but I also want it to display the error, stop the rest of the program from running, and restart the program. This is just a shortened version of my code which I have used as an example.

using System;

namespace Calculator
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            float input = 0;
            while (input != 5)
            {
                Console.Clear();
                Console.WriteLine("What would you like to do? Type: 1 for Addition. Write 5 to end program.");

                try
                {
                    input = float.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a number");
                }
                //Addition
                if (input == 1)
                {
                    Console.WriteLine("Enter First Value: ");
                    string FirstValue = Console.ReadLine();
                    float firstval = 0;
                    try
                    {
                        firstval = float.Parse(FirstValue);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Please enter a number");
                        break;
                    }
                    Console.WriteLine("Enter Second Value: ");
                    string SecondValue = Console.ReadLine();
                    float secval = 0;
                    try
                    {
                        secval = float.Parse(SecondValue);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Please enter a number");
                        break;
                    }
                    float sum = Add(firstval, secval);
                    Console.WriteLine("The sum is: {0}", sum);
                }
            }
        }

        public static float Add(float num1, float num2)
        {
            return num1 + num2;
        }
    }
}

When it says

                    catch (FormatException)
                    {
                        Console.WriteLine("Please enter a number");
                        break;
                    }

The break; makes it so the rest of the code stops, and it displays the error. That is good, but the program also ends after that, what I want, is that the program repeats after error. Is there any way that this could happen, but it allows 1) the Console.WriteLine("Please enter a number");, 2) the program to not run the rest of the code (The part where we are asked for a second value), and 3) the program to restart for the beginning. Please let me know if that didn't make sense, as it was a hard to explain. :)

Nox
  • 1
  • 2
    Does this answer your question? [Try-Catch with Do-While loop](https://stackoverflow.com/questions/49457724/try-catch-with-do-while-loop) – Martheen Jun 22 '21 at 03:02
  • 2
    Unrelated to your question... Instead of using `float.Parse` in a `try/catch`, use `float.TryParse`. Exceptions are heavy and should only be used for _exceptional_ circumstances. User's entering the wrong data are hardly exceptional. – Flydog57 Jun 22 '21 at 04:49

1 Answers1

0

It is very simple you do not need the "break" keyword because "break" terminates the loop execution to continue execution on exception use "continue" keyword without quotes the working fiddle is here

and your code after replacing "break" with "continue" is following

using System;

namespace Calculator
{
    public class Program
    {
        public static void Main(string[] args)
        {
            float input = 0;
            while (input != 5)
            {
                Console.Clear();
                Console.WriteLine("What would you like to do? Type: 1 for Addition. Write 5 to end program.");

                try
                {
                    input = float.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a number");
                }
                //Addition
                if (input == 1)
                {
                    Console.WriteLine("Enter First Value: ");
                    string FirstValue = Console.ReadLine();
                    float firstval = 0;
                    try
                    {
                        firstval = float.Parse(FirstValue);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Please enter a number");
                        continue;
                    }
                    Console.WriteLine("Enter Second Value: ");
                    string SecondValue = Console.ReadLine();
                    float secval = 0;
                    try
                    {
                        secval = float.Parse(SecondValue);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Please enter a number");
                        continue;
                    }
                    float sum = Add(firstval, secval);
                    Console.WriteLine("The sum is: {0}", sum);
                }
            }
        }

        public static float Add(float num1, float num2)
        {
            return num1 + num2;
        }
    }
}
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29
  • This is nice but, the continue loops makes it so my message of ```Console.WriteLine("Please enter a number");``` does not show, is there anyway that this can also be shown. – Nox Jun 22 '21 at 12:09
  • you can add a sleep function to wait for the message or simply remove Console.Clear() to not clear previous messages because you are clearing your old messages before new input also don't forget to accept answer if this is working to help others in this community .. – Zain Ul Abidin Jun 22 '21 at 14:07