我需要程序继续运行,并在引发异常后提示用户输入新值。
程序在遇到异常时抛出运行时错误,表示未处理
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");
}
}
}
“运行时错误,未处理的异常”
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;
}