在 catch 语句出错后重新启动程序

时间:2021-06-22 02:40:36

标签: c# try-catch

我在使用 catch() 函数捕获错误后尝试重新启动我的程序,但我也希望它显示错误,停止运行程序的其余部分,然后重新启动程序。 这只是我用作示例的代码的缩短版本。

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;
        }
    }
}

当它说

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

break; 使其余代码停止,并显示错误。那很好,但是程序也在那之后结束,我想要的是程序在错误之后重复。有没有可能发生这种情况,但它允许 1)Console.WriteLine("Please enter a number");,2)程序不运行其余代码(要求我们提供第二个值的部分),以及 3)程序重新开始。如果这没有意义,请告诉我,因为这很难解释。 :)

1 个答案:

答案 0 :(得分:0)

很简单,您不需要“break”关键字,因为“break”终止循环执行以继续执行异常使用“continue”关键字而不带引号工作小提琴是here

将“break”替换为“continue”后的代码如下

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;
        }
    }
}
相关问题