第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這不捕捉錯誤

為什么這不捕捉錯誤

C#
瀟湘沐 2021-07-19 16:08:33
當用戶輸入字符串而不是整數(shù)時,它會給我和錯誤并使控制臺崩潰。我不明白如何捕捉這個錯誤。如果發(fā)生此錯誤,我希望它做的是跳過玩家的回合。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);}
查看完整描述

3 回答

?
qq_花開花謝_0

TA貢獻1835條經(jīng)驗 獲得超7個贊

永遠不要Convert.ToInt32在用戶輸入上使用。

使用Int.TryParse來代替。

這是一個令人煩惱的例外的完美例子。


令人煩惱的異常是不幸的設(shè)計決策的結(jié)果。惱人的異常是在完全非異常的情況下拋出的,因此必須一直被捕獲和處理。


令人煩惱的異常的經(jīng)典示例是 Int32.Parse,如果給它一個無法解析為整數(shù)的字符串,它就會拋出異常。但是此方法的 99% 用例是轉(zhuǎn)換用戶輸入的字符串,這可能是任何舊事物,因此解析失敗絕不是例外。


Convert.ToInt32接受字符串作為參數(shù)的重載只是int.Parse在內(nèi)部調(diào)用-請參閱它的源代碼:


public static int ToInt32(String value) {

    if (value == null)

        return 0;

    return Int32.Parse(value, CultureInfo.CurrentCulture);

}

因此它和使用一樣令人煩惱int.Parse,應(yīng)該避免。經(jīng)驗法則是不適合的東西,你可以很容易地通過檢查代碼中使用的例外-例外是特殊的東西-主要的東西,你不能在你的代碼控制,如網(wǎng)絡(luò)的可用性和類似的東西-與用戶輸入adsf的,而不是12IS一點也不例外。


直接回答您的問題,

您沒有捕獲異常的原因是因為您Convert.ToInt32不在try塊內(nèi)。


要真正捕獲異常,您的代碼應(yīng)該如下所示:


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

int UserRowChoice = 0;

try 

{  

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

}

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

}

但是,正如我之前寫的,不要使用Convert.ToInt32-int.TryParse而是使用:


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

int UserRowChoice = 0;

if(int.TryParse(Console.ReadLine(), out UserRowChoice))

{  

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

    }

}

else

{

    ThrowError("You typed a string instead of an integer.  You've lost your turn."); 

    Console.ReadKey(); 

    RunGame(T1, CurrentPlayer, Winner); 

}


查看完整回答
反對 回復 2021-07-31
?
MMMHUHU

TA貢獻1834條經(jīng)驗 獲得超8個贊

您的try塊沒有圍繞可能引發(fā)InvalidCastException. 試試這個:


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

int UserRowChoice;

try 

{  

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

}

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

}


查看完整回答
反對 回復 2021-07-31
?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

在將值類型傳遞給您想要執(zhí)行的任何操作之前,請嘗試檢查該值類型。


string line = Console.ReadLine();

int value;

if (int.TryParse(line, out value))

    {

         Console.WriteLine("Integer here!");

    }

else

     {

         Console.WriteLine("Not an integer!");

     }

如果值是否為 int ,這將返回!如果它是一個int,繼續(xù)你想要的任何東西,否則,再次詢問使用。


查看完整回答
反對 回復 2021-07-31
  • 3 回答
  • 0 關(guān)注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號