2014-12-04 4 views
0

이것은 내 학교 프로젝트의 포커 게임입니다. 내가 알아낼 수없는 예외가있다.이 오류는 무엇입니까?

처음에는 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()); 
} 
} 

}

나는이 클래스의 예외를 만들었습니다. 그런 다음 Decks 클래스에서 예외 생성 오류가 계속 발생합니다.

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 루프에서 사용하면 (즉, < 3, j < 13), 예외 오류가 발생하는 이유는 무엇입니까?

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

답변

3

자바에서는 예외 코드 던져 처리해야한다. 이 컴파일러 오류는 Card 생성자가 MyPlayingCardException 유형의 예외를 throw 할 수 있지만 Deck 생성자가 처리하지 않고 있다는 것을 나타냅니다. 여기에는 두 가지 접근 방법이 있습니다. 중, 처리해야하는 방법 또는 인터페이스에 의해 선언 된 예외를 당신은

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

을 읽도록 Decks 생성자를 변경할 수 있습니다 또는 당신은 자바에서이

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

같은 예외를 처리 할 수 ​​있습니다 그것을 잡거나 다음 단계로 넘어 가기. 코드에서 Card() 생성자는 예외를 throw합니다. Decks() 생성자는 카드를 생성하지만 카드 예외를 catch하거나 throw하지 않습니다.

+0

Paul, Card()에 예외를 던지면 메모리로 이동하지 않아 다음 클래스에서 다시 던져서 잡아야한다는 말입니까? –

+0

RuntimeException이 아닌 한 어디서나 호출 경로를 따라 잡아서 던져야합니다. – PaulProgrammer

+0

나는 다음과 같이 try ... catch에 Card Card = new Card (j, i)를 사용한다. try {Card card = new Card (j, i); } catchcatch (MyPlayingCardException e) {System.out.println ("MyPlayingCardException :"+ e.getMessage());}하지만 그 뒤에있는 행에 오류가 발생합니다. 여기서 내가 실수 한거야? –

관련 문제