使用try catch和我的菜单程序

时间:2014-11-28 23:36:10

标签: c#

我创建了一个玩家类,并为我的菜单驱动的玩家系统制作了一个类的数组我尝试使用try catch捕获异常,如果为玩家编号,目标或助手输入的数字是负数,然后显示消息Number必须具有正值但由于某种原因它没有捕获异常。

任何帮助将不胜感激

//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)
    {
        try
        {
            number = IOConsole.GetInt32("\nCreate Player: please enter the player's number: ");
            //number = IOConsole.GetInt32(message);
            //(Console.ReadLine());
        }
        catch(Exception)
        {
            Console.WriteLine("\nNumber must have positive value");
        }
        if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount) == -1)
        {
            message =("\nCreate Player: please enter the player's First Name: ");
            firstName = IOConsole.Getstring(message);
                //Console.ReadLine();
            message = ("\nCreate Player: please enter the player's Last  Name: ");
            lastName = IOConsole.Getstring(message);
                //Console.ReadLine();
            try
            {
                goals = IOConsole.GetInt32("\nCreate Player: please enter the player's goals: ");

            }
            catch(Exception)
            {
                Console.WriteLine("\nNumber must have positive value");
            }
                //goals = IOConsole.GetInt32(message);
                //Int32.Parse(Console.ReadLine());
            try
            {
                assists = IOConsole.GetInt32("\nCreate Player: please enter the player's assists: ");
                //assists = IOConsole.GetInt32(message);
                //Console.ReadLine();
            }
            catch (Exception)
        {
            Console.WriteLine("\nNumber must have positive value");
        }

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

}
  

如果需要,这是我的Player类中的数字,目标和助手

//Public Number accessor
public Int32 Number
{
    get
    {
        //Return member variable value
        return _number;
    }
    set
    {
        //Validate value and throw exception if necessary
        if (value <=  0)
            throw new Exception("Invalid Player Number");
        else
            //Otherwise set member variable value
            _number = value;
    }
}

//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;
    }
}



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

1 个答案:

答案 0 :(得分:0)

根据我在此代码中的说法,您Int32类的Player属性为NumberGoalsAssists,其中你在分配时抛出异常。您正在尝试在读取输入时捕获异常;但是,您输入的内容只是存储在标准Int32变量中。在将这些变量分配给播放器的实例(稍后发生在这些catch语句之外)之前,您所抛出的异常不会发生。该应用程序可能仍在抛出异常,但它们发生在InsertPlayer方法内,而不是列出catch语句的方法。

此外,将变量传递给此函数似乎很奇怪,只是为了用控制台数据填充这些变量。值来自函数调用或来自控制台,但不是两者。在我看来,你不需要函数签名中的变量,它们可以在本地声明。