尝试捕获不捕获的感知 [英] Try Catch not catching excption

查看:80
本文介绍了尝试捕获不捕获的感知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的菜单驱动的玩家系统创建了一个玩家类并从该类中创建了一个数组,如果用户在目标中输入负数,则会抛出异常。我设置了一个try catch来捕获异常,但是当我将一个负数放入目标时,它一直说Exception是unhanded SystemPlayer.exe中发生了未处理的类型'System.Exception'的异常附加信息:目标必须是正数。我不确定如何解决这个问题



任何帮助将不胜感激



I created a player class and made a array from that class for my menu driven player system, if the user enters a negative number into goals it throws a exception. I put in a try catch to catch the exception but when I put a negative number into goals it keeps saying Exception was unhanded An unhandled exception of type 'System.Exception' occurred in SystemPlayer.exe Additional information: Goals must be a positive number. I am not sure on how to fix this

any help would be appreciated

//Creates a player in the tables if the array is not already full and the name is not a duplicate
        static void ProcessCreate(Int32 number, String firstName, String lastName, Int32 goals,
            Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)

        {
            string message;
            //Int32 player = 0;

            if (playerCount < MAXPLAYERS)
            {
              
                    number = IOConsole.GetInt32("\nCreate Player: please enter the player's number: ");
                    //number = IOConsole.GetInt32(message);
                    //(Console.ReadLine());
               if(number < 0)
                {
                    Console.WriteLine("\nNumber must have positive value");
                }
                else if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount) == -1)
                {
                    try
                    {
                        Console.WriteLine("\nCreate Player: please enter the player's First Name: ");
                        firstName = Console.ReadLine();

                        
                    }
                    catch(Exception)
                    {
                        Console.WriteLine("Player first name must have a value");
                    }
                      //Console.ReadLine();
                    try
                    {
                        Console.WriteLine("\nCreate Player: please enter the player's Last  Name: ");
                        lastName = Console.ReadLine();

                       
                    }
                    catch(Exception)
                    {
                        Console.WriteLine("Player last name must have a value");
                    }
                    //lastName = IOConsole.Getstring(message);
                     //Console.ReadLine();
                    try
                    { 
                   
                        goals = IOConsole.GetInt32("\nCreate Player: please enter the player's goals: ");
                                    
                        
                    }
                    catch(Exception)
                    {
                        Console.WriteLine("\nGoals must have positive value");
                    }
                    if(goals < 0)
                    {
                        Console.WriteLine("\nGoals must have positive value");
                    }
                        //goals = IOConsole.GetInt32(message);
                    
                    

                    else 
                    {
                        assists = IOConsole.GetInt32("\nCreate Player: please enter the player's assists: ");
                        //assists = IOConsole.GetInt32(message);
                        //Console.ReadLine();
                    }

                    if (assists < 0)
                {
                    Console.WriteLine("\nAssist must have a positive value");
                }
                    else 
                    InsertPlayer(number, firstName, lastName, goals, assists, players, ref playerCount);
                    Console.WriteLine("\n{0,7}   {1,-20}{2, -20}{3,8}{4,8}{5,8}\n", "Number", "First Name", "Last Name", "Goals", " Assists", "Points");
                    for (Int32 player = 0; player < playerCount; player++)
                    Console.WriteLine("{0,7}   {1,-20}{2, -20}{3,8}{4,8}{5,8}",
                  players[player].Number, players[player].FirstName, players[player].LastName,
                  players[player].Goals, players[player].Assists, players[player].Points());
                
                  
                    Console.WriteLine();
                }
                else
                    Console.WriteLine("\nCreate Player: the player number already exists");
            }
            else
                Console.WriteLine("\nCreate Player: the player roster is already full");

        }



这是我的播放器类中抛出异常的地方


Here is where the exception was thrown in my player class

//Public Goals accessor
       public Int32 Goals
       {
           get
           {
               //Return member variable value
               return _goals;
           }
           set
           {
               //Validate value and throw exception if necessary
               if (value < 0)
                   throw new Exception("Goals must be a positive number");
               else
                   //Otherwise set member variable value
                   _goals = value;
           }
       }

推荐答案

嗯......你知道尝试什么...抓住了,不是吗?

它发现运行时错误并提醒您的代码解决问题。它不会检查用户输入 - 因为无论用户输入什么,都不会导致异常!

所以这个:

Um...You do know what try...catch does, don't you?
It finds run time errors and alerts your code to the problem. It doesn't check user input - because whatever the user might input, it isn't an error that will cause an exception!
So this:
try
{
    Console.WriteLine("\nCreate Player: please enter the player's First Name: ");
    firstName = Console.ReadLine();
}
catch(Exception)
{
    Console.WriteLine("Player first name must have a value");
}



只有在(某种程度上)WriteLine或ReadLine操作失败时才会显示消息。只有在发生真正的IO错误时才会发生这种情况 - 在这种情况下,尝试使用Console.WriteLine来报告它的可能性也会很大......



No.

停止尝试使用try ... catch,并检查实际值。


Will only show a message if (somehow) the WriteLine or ReadLine operations failed. Which will only happen if a genuine IO error occurred - in which case, the chances are that trying to use Console.WriteLine to report it is going to fail as well...

No.
Stop trying to use try...catch for this, and check the actual values.

Console.WriteLine("\nCreate Player: please enter the player's First Name: ");
firstName = Console.ReadLine();
if (string.IsNullOrWhiteSpace(firstName))
    {
    Console.WriteLine("Player first name must have a value");
    // And don't create the user...
    }

不要忘记所有其他的......

Don't forget all the other ones, as well...


这篇关于尝试捕获不捕获的感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆