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

C# 如何获取自定义异常以显示导致异常的输入?,c#,visual-studio,oop,exception-handling,C#,Visual Studio,Oop,Exception Handling,我制作了这个程序,这是一个猜谜游戏,除了一件事,一切都很好。我创建了一个自定义异常,用于检查输入类型是否仅为字母。我已经对它进行了测试,它确实按照预期抛出了异常,但我希望异常在其消息中显示用户键入的导致异常的字符。这样做的最佳方式是什么?这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; us

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

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

        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用静态系统控制台;
名称空间测试{
阶级刽子手
{
静态void Main(字符串[]参数)
{
字符串hiddenWord=“chivarly”;
字符串占位符;
char[]a=新字符[hiddenWord.Length];
for(int i=0;i
您可以将其包含在异常消息中

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

您只需将输入传递给构造函数

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

Exception
是一个类,您需要确保在一天结束时传递您想要在层次上显示的任何信息。通常的方法是使用
innerException
Exception是一个类,您需要确保在一天结束时传递任何信息要显示层次结构,常用的方法是使用
innerException