我刚才用Python为老师写了一个控制台应用程序,这是通常的评分计算。这段代码阻止了无效输入:
while True:
try:
Paper2 = float(input("Enter grade: "))
except ValueError:
print ("༼ つ ◕_◕ ༽つ error!")
continue
if Paper2 > 10:
print ("༼ つ ◕_◕ ༽つ error!")
continue
else:
break
C#中的Try/except ValueError
怎么样?
答案 0 :(得分:1)
float Paper2;
while (true)
{
try
{
Paper2 = float.Parse(Console.ReadLine());
if (Paper2 > 10)
{
throw new Exception();
}
else
{
break;
}
}
catch
{
Console.WriteLine("༼ つ ◕_◕ ༽つ error!");
}
}
答案 1 :(得分:0)
每Python Doc Try/except ValueError
用于异常处理。您使用C#
阻止{/ 1}}在try .. catch
中执行相同操作
try
{
// your code logic which may raise exception
}
catch(Exception ex)
{
//handle the exception do necessary stuff like logging
}
finally
{
//perform cleanup here
}