简体   繁体   English

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

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

I made this program that is a guessing game of sorts and it all works well except for one thing. 我做了这个程序,这是一个各种猜谜游戏,除了一件事,它一切正常。 I created a custom exception that checks if input type is that of letters only. 我创建了一个自定义异常,检查输入类型是否仅为字母。 I have tested it and it does throw the exception as expected, but I would like the exception to show in it's message what character the user typed cause the exception. 我已经对它进行了测试,并且确实按预期抛出异常,但我希望异常在其消息中显示用户键入的字符导致异常。 What is the best way of doing this? 这样做的最佳方式是什么? Here is my code: 这是我的代码:

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

        }
    }

}

You can include it in the exception message 您可以将其包含在异常消息中

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

    }
}

...and used like ...并使用像

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

You should also read up on some good coding practices when it come to custom exceptions: 当涉及自定义异常时,您还应该阅读一些好的编码实践:

You just pass in the input to the constructor. 您只需将输入传递给构造函数。

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

    }
}

Calling it like so: 这样称呼它:

onLetterException nle = new NonLetterException(c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM