2016-04-28 5 views
1

私はこの種のゲームを推測しています。入力の型が文字のみであるかどうかを調べるカスタム例外を作成しました。私はそれをテストし、期待どおりの例外をスローしますが、私は例外が発生した文字をユーザーが入力した文字をメッセージに表示したいと思います。これを行う最善の方法は何ですか?あなたはまた、いくつかの良いコーディング慣行上に読んでくださいあなたは例外メッセージどのような入力が例外を引き起こしたかを示すためにカスタム例外を取得するにはどうすればよいですか?

class NonLetterException : Exception 
{ 
    private static string msg = "Error input ({0}) is not of the alpahbet. "; 
    public NonLetterException(char c) : base(string.Format(msg, new String(c,1))) 
    { 

    } 
} 

に含めることができ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using static System.Console; 

namespace Test { 
    class Hangman 
    { 

     static void Main(string[] args) 
     { 


      string hiddenWord = "chivarly"; 
      string placeHolder; 

      char[] a = new char[hiddenWord.Length]; 

      for (int i = 0; i < a.Length; i++) 
      { 
       a[i] = '*'; 
      } 

      for (int i = 0; i < a.Length; i++) 
      { 
       Console.Write(a[i] + " "); 
      } 

      Console.WriteLine("Welcome try to guess my word!"); 

      int count = 0; 
      do 
      { 
       Console.WriteLine("Enter your guess letter"); 
       char input = Console.ReadLine().ToCharArray()[0]; 
       placeHolder = Convert.ToString(input); 

       try 
       { 
        bool isCharLetter = CheckLetter(placeHolder); 
       } 
       catch(NonLetterException x) 
       { 
        WriteLine(x.Message); 
        WriteLine(x.StackTrace); 
       } 

       for (int i = 0; i < hiddenWord.Length; i++) 
       { 

        if (hiddenWord[i] == input) 
        { 
         count++; 
         a[i] = input; 

         for (int j = 0; j < a.Length; j++) 
         { 
          Console.Write(a[j] + " "); 
         } 
        } 

       } 
       Console.WriteLine(); 
      } 

      while (count < a.Length); 
      Console.WriteLine("You have won, HUZZAH!"); 
      Console.ReadLine(); 
     } 

     static bool CheckLetter(string questionedChar) 
     { 
      bool decision = false; 
      foreach(char c in questionedChar) 
      { 
       if(!char.IsLetter(c)) 
       { 
        decision = false; 
        NonLetterException nle = new NonLetterException(); 
        throw (nle); 
       } 
       else 
       { 
        decision = true; 
       } 
      } 
      return decision; 
     } 

    } 

    class NonLetterException : Exception 
    { 
     private static string msg = "Error input string is not of the alpahbet. "; 
     public NonLetterException() : base(msg) 
     { 

     } 
    } 

} 
+0

'Exception'はクラスであり、あなたが一日の終わりに、あなたは階層 –

+0

をdispalyしたいものは何でも情報に沿って渡すことを確認する必要がありますこれを行う一般的な方法は、 'innerException'を使うことです。 –

答えて

0

...と

//...other code 
static bool CheckLetter(string questionedChar) 
{ 
    bool decision = false; 
    foreach(char c in questionedChar) 
    { 
     if(!char.IsLetter(c)) 
     { 
      decision = false; 
      throw new NonLetterException(c); 
     } 
     else 
     { 
      decision = true; 
     } 
    } 
    return decision; 
} 
//...other code 

のように使用:ここに私のコードですカスタム例外になると:

0

あなただけのコンストラクタに入力を渡します。

そのようにそれを呼び出す
class NonLetterException : Exception 
{ 
    private static string msg = "Error input string is not of the alpahbet:"; 
    public NonLetterException(string input) : base(msg + input) 
    { 

    } 
} 

onLetterException nle = new NonLetterException(c); 
関連する問題