如何获取自定义异常以显示导致异常的输入?

时间:2016-04-28 02:14:05

标签: c# visual-studio oop exception-handling

我制作了这个程序,这是一种各种猜谜游戏,除了一件事情外它一切正常。我创建了一个自定义异常,检查输入类型是否仅为字母。我已经对它进行了测试,并且确实按预期抛出异常,但我希望异常在其消息中显示用户键入的字符导致异常。这样做的最佳方式是什么?这是我的代码:

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

        }
    }

}

2 个答案:

答案 0 :(得分:0)

您可以将其包含在例外消息

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

当涉及自定义异常时,您还应该阅读一些好的编码实践:

答案 1 :(得分: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);