是什么让我这个错误?

时间:2014-12-04 22:57:53

标签: java exception-handling

这是我学校项目的扑克游戏。我有一些例外,我无法弄明白。

一开始,我定义了Card类。

class Card {

/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

/* Data field of a card: rank and suit */
private int cardRank;  /* values: 1-13 (see Rank[] above) */
private int cardSuit;  /* values: 0-3  (see Suit[] above) */

/* Constructor to create a card */
/* throw MyPlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws MyPlayingCardException { 
if ((rank < 1) || (rank > 13))
    throw new MyPlayingCardException("Invalid rank:"+rank);
else
        cardRank = rank;
if ((suit < 0) || (suit > 3))
    throw new MyPlayingCardException("Invalid suit:"+suit);
else
        cardSuit = suit;
}

/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }


/* Few quick tests here */
public static void main(String args[])
{
try {
    Card c1 = new Card(1,3);    // A Spades
    System.out.println(c1);
    c1 = new Card(10,0);    // 10 Clubs
    System.out.println(c1);
    c1 = new Card(10,5);        // generate exception here
}
catch (MyPlayingCardException e)
{
    System.out.println("MyPlayingCardException: "+e.getMessage());
}
}

}

我在这个类中创建了Exception。然后,在我的Decks类中,我继续从Deck构造函数中获取异常错误。

class Decks {

/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;   

/* this starts with n*52 cards deck from original deck   */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck      */
private List<Card> dealDecks;

/* number of decks in this object */
private int numberDecks;


/**
 * Constructor: Creates default one deck of 52 playing cards in originalDecks and
 *          copy them to dealDecks.
 *              initialize numberDecks=n
 * Note: You need to catch MyPlayingCardException from Card constructor
 *       Use ArrayList for both originalDecks & dealDecks
 */
public Decks()
{
    // implement this method!

    ArrayList<Card> originalDecks = new ArrayList<Card>(52);
    ArrayList<Card> dealDecks = new ArrayList<Card>(52); 

    for (int i=0; i<=3; i++)  {

        for (int j=0; j<= 13; j++)  {

                Card card = new Card(j,i);  
                originalDecks.add(card); 
            }               
        }

    dealDecks.addAll(originalDecks); 


}

为什么?如果我使用我的实例卡是来自for循环(这意味着他们&lt; 3,j&lt; 13),为什么我仍然得到异常错误?

unreported exception MyPlayingCardException; must be caught or declared to be thrown
                Card card = new Card(j,i);  

2 个答案:

答案 0 :(得分:3)

在java中,您必须处理代码可能抛出的异常。此编译器错误表示您的Card构造函数可以抛出类型MyPlayingCardException的异常,但您的Deck构造函数无法处理它。这里有两种方法。您可以将Decks构造函数更改为

public Decks() throws MyPlayingCardException
{
    //constructor code here
}

或者您可以像这样处理

for (int j=0; j<= 13; j++)  {

            try
            {
            Card card = new Card(j,i);  
            originalDecks.add(card); 
            }
            catch(MyPlayingCardException ex){
                //some code to handle MyPlayingCardException here
            }
        }               
    }

答案 1 :(得分:1)

在java中,必须通过捕获它或将其传递到下一级别来处理由方法或接口声明的异常。在您的代码中,Card()构造函数抛出异常。您的Decks()构造函数会创建卡片,但不会捕获或抛出卡片异常。