猿问

为什么这不捕捉错误

当用户输入字符串而不是整数时,它会给我和错误并使控制台崩溃。我不明白如何捕捉这个错误。如果发生此错误,我希望它做的是跳过玩家的回合。


Console.Write("Row (1-3): ");

int UserRowChoice = Convert.ToInt32(Console.ReadLine());

try {  }

catch (InvalidCastException e) { ThrowError("You typed a string instead of an integer.  You've lost your turn."); Console.ReadKey(); RunGame(T1, CurrentPlayer, Winner); }

if (UserRowChoice < 1 || UserRowChoice > 3)

{

    ThrowError("You either typed a number that was less than 1 or greater than 3.  You've lost your turn.");

    Console.ReadKey();

    RunGame(T1, CurrentPlayer, Winner);

}


潇湘沐
浏览 145回答 3
3回答

qq_花开花谢_0

永远不要Convert.ToInt32在用户输入上使用。使用Int.TryParse来代替。这是一个令人烦恼的例外的完美例子。令人烦恼的异常是不幸的设计决策的结果。恼人的异常是在完全非异常的情况下抛出的,因此必须一直被捕获和处理。令人烦恼的异常的经典示例是 Int32.Parse,如果给它一个无法解析为整数的字符串,它就会抛出异常。但是此方法的 99% 用例是转换用户输入的字符串,这可能是任何旧事物,因此解析失败绝不是例外。Convert.ToInt32接受字符串作为参数的重载只是int.Parse在内部调用-请参阅它的源代码:public static int ToInt32(String value) {&nbsp; &nbsp; if (value == null)&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; return Int32.Parse(value, CultureInfo.CurrentCulture);}因此它和使用一样令人烦恼int.Parse,应该避免。经验法则是不适合的东西,你可以很容易地通过检查代码中使用的例外-例外是特殊的东西-主要的东西,你不能在你的代码控制,如网络的可用性和类似的东西-与用户输入adsf的,而不是12IS一点也不例外。直接回答您的问题,您没有捕获异常的原因是因为您Convert.ToInt32不在try块内。要真正捕获异常,您的代码应该如下所示:Console.Write("Row (1-3): ");int UserRowChoice = 0;try&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp; UserRowChoice = Convert.ToInt32(Console.ReadLine());}catch (InvalidCastException e) { ThrowError("You typed a string instead of an integer.&nbsp; You've lost your turn."); Console.ReadKey(); RunGame(T1, CurrentPlayer, Winner); }if (UserRowChoice < 1 || UserRowChoice > 3){&nbsp; &nbsp; ThrowError("You either typed a number that was less than 1 or greater than 3.&nbsp; You've lost your turn.");&nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; RunGame(T1, CurrentPlayer, Winner);}但是,正如我之前写的,不要使用Convert.ToInt32-int.TryParse而是使用:Console.Write("Row (1-3): ");int UserRowChoice = 0;if(int.TryParse(Console.ReadLine(), out UserRowChoice)){&nbsp;&nbsp;&nbsp; &nbsp; if (UserRowChoice < 1 || UserRowChoice > 3)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ThrowError("You either typed a number that was less than 1 or greater than 3.&nbsp; You've lost your turn.");&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; &nbsp; &nbsp; RunGame(T1, CurrentPlayer, Winner);&nbsp; &nbsp; }}else{&nbsp; &nbsp; ThrowError("You typed a string instead of an integer.&nbsp; You've lost your turn.");&nbsp;&nbsp; &nbsp; Console.ReadKey();&nbsp;&nbsp; &nbsp; RunGame(T1, CurrentPlayer, Winner);&nbsp;}

MMMHUHU

您的try块没有围绕可能引发InvalidCastException. 试试这个:Console.Write("Row (1-3): ");int UserRowChoice;try&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp; UserRowChoice = Convert.ToInt32(Console.ReadLine());}catch (InvalidCastException e) { ThrowError("You typed a string instead of an integer.&nbsp; You've lost your turn."); Console.ReadKey(); RunGame(T1, CurrentPlayer, Winner); }if (UserRowChoice < 1 || UserRowChoice > 3){&nbsp; &nbsp; ThrowError("You either typed a number that was less than 1 or greater than 3.&nbsp; You've lost your turn.");&nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; RunGame(T1, CurrentPlayer, Winner);}

慕码人2483693

在将值类型传递给您想要执行的任何操作之前,请尝试检查该值类型。string line = Console.ReadLine();int value;if (int.TryParse(line, out value))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine("Integer here!");&nbsp; &nbsp; }else&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine("Not an integer!");&nbsp; &nbsp; &nbsp;}如果值是否为 int ,这将返回!如果它是一个int,继续你想要的任何东西,否则,再次询问使用。
随时随地看视频慕课网APP
我要回答