簡體   English   中英

如何獲取自定義異常以顯示導致異常的輸入?

[英]How do I get my custom exception to show what input caused the exception?

我做了這個程序,這是一個各種猜謎游戲,除了一件事,它一切正常。 我創建了一個自定義異常,檢查輸入類型是否僅為字母。 我已經對它進行了測試,並且確實按預期拋出異常,但我希望異常在其消息中顯示用戶鍵入的字符導致異常。 這樣做的最佳方式是什么? 這是我的代碼:

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

        }
    }

}

您可以將其包含在異常消息中

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

    }
}

...並使用像

//...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

當涉及自定義異常時,您還應該閱讀一些好的編碼實踐:

您只需將輸入傳遞給構造函數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM