Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9275559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:36:59+00:00 2026-06-18T16:36:59+00:00

Thanks for checking out my question. I have the following code for a java

  • 0

Thanks for checking out my question. I have the following code for a java deck. I want to step away from arrays and toy code and try using best practice and object oriented principles for this one, I’m aware that I can do this in a much simpler, but less re-usable, manner.

The end goal is to create a cardgame framework which I can use to deal with the mundane parts of deck management while concentrating on the implementation of different games.

I’m having an issue with my error handling. My idea for the draw() code is as follows –

1) If there’s another card return it and move the iterator along. This will eliminate the need for a discard pile as the discards will be behind the iterator with the .last() card being the one just drawn.

2) If there isn’t another card and “cards” is empty run the emptyDeck() method. This method will be implemented in subclasses. For example in solitaire you may want to end the game after running through the deck x number of times so you may not want to draw a card any more.

3) if the deck isn’t empty and you have no more cards then you call the endOfDeck() method which is going to be subclassed. Again, you may want to shuffle the deck or simply reset the iterator

However I’m getting the old “must return a Card” error message. I’ve tried creating a custom exception but I can only specify one handler method. Can anyone suggest a smart way to do this?

    public abstract class Deck 
{

private ArrayList<Card> cards;
private ListIterator<Card> deckPosition = cards.listIterator();
/**
 * 
 */
public Deck() 
{   
}

public Card draw()
{
    Card drawn;

    try
    {
        if(deckPosition.hasNext())
        {
            drawn = deckPosition.next();
        }
        else if(cards.isEmpty())
        {
            emptyDeck();
        }
        else
        {
            endOfDeck();
        }
    }

    catch(Exception e)
    {
        System.out.println("Exception when drawing a card, check try/catch block in draw() method of Deck");
        e.printStackTrace();
    }

    finally
    {
        return drawn;
    }
}

public abstract void endOfDeck();
public abstract void emptyDeck();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:37:00+00:00Added an answer on June 18, 2026 at 4:37 pm

    When a method is unable to return a meaningful result due to an internal error, the method should throw an exception instead of returning just something. So when an error occurs in your draw method which the method itself can not manage, it should throw an exception itself which is then handled by the caller.

    In this case I would create a new exception class DeckEmptyException. When the deck is empty, the draw method would throw that exception instead of returning a card. Whoever calls the draw method would then have to deal with this exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the conversion from int into long in java. Why
Hello and thanks for checking out my question. <meta property=og:image content=http://www.myurl.com/images/test.png/> I have an
Thanks for checking out my question. I am trying to only update a value
I have the following code public ClassToTest : IClassToTest { private readonly DBRepository rep;
Greetings everyone and thank you for checking out my post. I am trying to
Thanks for reading this. I have no idea why this is throwing a NullReferenceException
Thanks in advance for your help. I have a need within an application to
I was checking out the Google Maps Data API and I was wondering if
I use Vim's spell checking to validate texts in Russian. We have letter ё
Beginner here, I have a simple question. In Android what would be the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.