-1

The method I want to add error trapping to is :

private void DealCard() {
    Card c = deck.removeTopCard();
    onTable.add(c);
    System.out.println(c);
}

It allows you to press a case in the GUI, and then prints a single card out on the deck (table array list). You can keep pressing this case to deal more cards from the deck. How can I add error trapping so that once the deck of all 52 cards has been dealt, it brings up a message such as "whole deck dealt", because currently it just breaks after you deal all the cards and then attempt to deal another one.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • the error trapping could begin inside `removeTopCard()` which should imo return `null` if there is no card to remove. Then if `c` is `null` show your message – chancea May 07 '15 at 15:16
  • Why don't you just consider to add a method to count remaining cards of deck and testing this value before removing a new card ? – Fabien Thouraud May 07 '15 at 15:17
  • Thanks, will keep options open with it but the try statement below seems to handle the problem easily enough. – lewis Rossiterlk May 07 '15 at 15:20
  • Also just to let you know the downvotes are because many think there's lack of research effort as it is a basic question, but I understand it's hard to research on concepts you do not know the terms for. Error handling is a better term than error trapping. :) – matrixanomaly May 07 '15 at 15:25

2 Answers2

0

private void DealCard() {

  try {
      Card c = deck.removeTopCard();
      onTable.add(c);
      System.out.println(c);
  } catch(Exception ex) {
      // handle the exception here
  }

}
Jeff Anderson
  • 799
  • 7
  • 18
0

You'll want a if condition that checks the number of card you have inside the deck (which I assume if an arraylist of Card) to see if you have anymore cards. Here's an example:

private void DealCard() {

  if(!deck.isEmpty())
  {
     Card c = deck.removeTopCard();
     onTable.add(c);
     System.out.println(c);
  }
  else 
  {
     //print something to notify user that the decks is empty, or someother form of handling.
  }
}

Another option is to put the if-else conditional inside the removeTopCard() method, this is more of a programming principle where you have Seperation of Concerns that ensure one method does only one thing, and checking for empty decks might be a job for removeTopCard(), this is your choice, though.

I would recommend doing error checking rather than catching/throwing exceptions as one should only throw errors when they are truly exceptional, having an empty deck seems like an issue you'll run into fairly often, and not a super special case, so you should handle it. Throwing exceptions are costly, which might not matter to you if you're just a beginner and starting out, (classes I took don't really care) but you want to think about this if you'll be programming in the long run. Here's an SO question with more information on that. You might want to read up more about concepts such as if-else conditionals, handling exceptions and null values and what not for additional knowledge.

Community
  • 1
  • 1
matrixanomaly
  • 6,627
  • 2
  • 35
  • 58