2017-11-14 15 views
-4

キャッチフォーマットの例外をテストしたいが、単語を入力しようとするとプログラムがクラッシュする。私のプログラムはクラッシュすることなく例外をキャッチできますか?フォーマット例外 - キャッチと試し

string words; 
int[] number; 
int i = 0; 

while (true) 
{ 
    Console.WriteLine("How many numbers would you like to enter? "); 
    words = Console.ReadLine(); 

    if (isInteger(words)) 
    { 
     number = new int[Convert.ToInt32(words)]; 
     Console.WriteLine("Please enter your numbers: "); 

     for (i = 0; i < number.Length; ++i) 
     { 
      number[i] = Convert.ToInt32(Console.ReadLine()); 
      isInteger(words); 
     } 
    } 
} 

isInteger方法:

private static bool isInteger(string words) 
{ 
    try 
    { 
     return true; 
    } 
    catch (FormatException) 
    { 
     return false; 
    } 
} 
+1

'try'ブロックで' return true; 'が実行されない' catch'ブロックを引き起こします。 'words'が数値でない場合、' FormatException'を投げるtryブロックの中で 'int.Parse(words)'を使ってチェックできます。 –

+0

これは、すべてのステートメントが値(真、偽など)を返さなければならないと言います。 –

+1

'try {var number = int.Parse(words);真を返します。 } '=>' words'が数値でない場合、 'FormatException'を投げてから' return false'を実行します。 'if(int.TryParse(words、out int number)){trueを返します。 } 'しかし、' FormatException'を投げていません。 –

答えて

0

あなたは彼らがトライcatchブロック内にラップする必要がConvertConvert.ToInt32(words)またはConvert.ToInt32(Console.ReadLine())への呼び出しによってスローされた例外をキャッチします。すべてのキャッチは、前のtry内で発生する例外のみを処理します。したがって、たとえば

ため
try 
{ 
    number = new int[Convert.ToInt32(words); 
} 
catch (FormatException ex) 
{ 
    // Do something here ex is needed if you want to look at the exception 
} 

それとも、1トライキャッチでそれをすべてを包むことができ

try 
{ 
    number = new int[Convert.ToInt32(words); 
    Console.WriteLine("Please enter your numbers: "); 

    for (i = 0; i < number.Length; ++i) 
    { 
    number[i] = Convert.ToInt32(Console.ReadLine()); 
    isInteger(words); 
    } 
} 
catch (FormatException ex) 
{ 
    // Do something here ex is needed if you want to look at the exception 
} 

isIntegerそれがないすべてがどの決して除き、trueを返しているので、処理するための例外を持っていることはありません。関数内で例外を処理したい場合は、関数がcatch以外の呼び出しを行うか、catchブロックで呼び出さなければなりません。

0

次のコードは、あなたが目指していたものである例外を扱う練習をしたい場合は... int.TryParse():に似たもの

void Main() 
{ 
    string words; 
    int[] array; 
    int number; 
    int i = 0; 

    while (true) 
    { 
     Console.WriteLine("How many numbers would you like to enter? "); 
     words = (Console.ReadLine()); 

     if (TryParse(words,out number)) 
     { 
      array = new int[number]; 

      Console.WriteLine("Please enter your numbers: "); 

      for (i = 0; i < array.Length; ++i) 
      { 
       if(!TryParse(Console.ReadLine(), out array[i])){ 
        Console.WriteLine("You need to enter an integer\nPlease Try again:"); 
        i--; 
       } 
      } 
      Console.WriteLine(string.Join("\n",array)); 
     } 
    } 
} 

C#の方法は、通常、パスカルケースです...それはちょうど慣習です言語Javaの規約はcamelCaseです。以下のソースhere. 「アウト」に見られるように、あなたは、C#を学ぶ上でparameter

private static bool TryParse (string input, out int result) 
{ 
    result = default;//if you are using C#7 otherwise result = default(int); Default is a keyword that sets the variable to the default value of that type...in this case default(int) == 0 
    try 
    {//you need to place the throwable code inside a try catch block...otherwise you wont catch the exception. 
     result = int.Parse(input);//int.Parse and Convert.ToInt32 can throw the FormatException 
    } 
    catch (FormatException) //You correctly caught the exception 
    { 
      return false; 
    } 
    return true; 
} 

おめでとうために結果を設定することができますC#のキーワードです。私はあなたが言語を楽しんで欲しい!