当代码碰到异常情况时程序立即停止,需要程序在异常抛出后重新提示用户

问题描述 投票:0回答:1

我需要程序继续运行,并在引发异常后提示用户输入新值。

程序在遇到异常时抛出运行时错误,表示未处理

using System;
using static System.Console;
class SwimmingWaterTemperature
{
    static void Main()
    {
        // Your code here
        int temp = 0;
        Console.WriteLine("Enter temperature or type 999 to quit");
        do
        {
            temp = Convert.ToInt32(Console.ReadLine());
            bool comfort = CheckComfort(temp);
        }
        while (temp != 999);

    }

    public static bool CheckComfort(int temp)
    {
        // your logic here   
            bool comfort;
            if (temp >= 70 && temp <= 85)
            {
                Console.WriteLine("{0} Degrees is comfortable for swimming.", temp);
                Console.WriteLine("Enter another temperature or 999 to quit");
            comfort = true;
                return comfort;
            }
            else if ((temp >= 32 && temp <= 70) || (temp >= 85 && temp <= 212) && temp != 999)
            {
                Console.WriteLine("{0} Degrees is not comfortable for swimming.", temp);
                Console.WriteLine("Enter another temperature or 999 to quit");
            comfort = false;
                return comfort;
            }
            else if (temp == 999)
            {
            comfort = false;
            return comfort;
            }
            else
            {
                throw new System.ArgumentException("Value does not fall within expected range");
                Console.WriteLine("Enter another temperature or 999 to quit");
        }

    }
}

“运行时错误,未处理的异常”

c#
1个回答
0
投票
try
            {
                throw new System.ArgumentException();
            }
            catch (ArgumentException)
            {
                Console.WriteLine("Value does not fall within the expected range.");
                Console.WriteLine("Enter another temperature or 999 to quit");
                comfort = false;
                return comfort;
            }
© www.soinside.com 2019 - 2024. All rights reserved.