简体   繁体   中英

Try catch not catching exception with input

I am creating a blackjack game and so far I have made a card class. The card class works, but when I go to test my card class when it does test3 the exception should catch because "X" is not in the values array but for some reason it doesn't catch and displays X instead of the error message "Invalid Input". What I want to happen is my Value set accessor should search the String[]values array and then determine if the value argument is valid or not and if it isn't then throw a new exception. I am not sure on how to fix this. Also I can't use a enum for my values I need to use the values array.

any help would be appreciated

Here is what I have for my card class

class Card
{

    public enum SUIT { HEARTS, SPADES, DIAMONDS, CLUBS };

    private SUIT _suit;
    private String _value;

    public Card(SUIT suit, String value)
    {

        _suit = suit;
        _value = value;
    }

    public SUIT Suit
    {
        get
        {
            //Return member variable value
            return _suit;
        }
        set
        {
            _suit = value;
        }
    }

    private String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
    public String Value
    {
        get { return _value; }
        set
        {
            if (!values.Contains(value))
            {
                throw new ArgumentException("Invalid Input");
            }
            _value = value;
        }
    }
}

Here is the code I was testing with

try
        {
            Card testCard1 = new Card(Card.SUIT.SPADES, "Q");
            Console.WriteLine(testCard1.Suit + " " + testCard1.Value);
            Card testCard2 = new Card(Card.SUIT.DIAMONDS, "10");
            Console.WriteLine(testCard2.Suit + " " + testCard2.Value);
            Card testCard3 = new Card(Card.SUIT.DIAMONDS, "X");
            Console.WriteLine(testCard3.Suit + " " + testCard3.Value);

        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e.Message);
        }
        Console.ReadLine();

You are assigning the incorrect value via the constructor to the backingfield. This isnt a bad thing todo, however it bypasses your propertylogic.

public Card(SUIT suit, String value)
{

    _suit = suit;
    _value = value; // this assignes the value directly to the field, bypassing your property-logic.
}

Change it like this, would be the easiest solution

public Card(SUIT suit, String value)
{

    _suit = suit;
    Value = value; // use the property here.
}

You have the check logic for Value written in the set , but in your code you are passing X in the constructor, which is assigning to the private member variables (thereby completely avoiding that nice filter logic you wrote).

You should assign to the properties in the constructor like this:

public Card(SUIT suit, String value)
{
    Suit = suit;
    Value = value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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