如何处理返回对象和处理错误 [英] How to deal with returning an object and handling errors

查看:36
本文介绍了如何处理返回对象和处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您查看我的问题.我有以下 Java 平台代码.我想摆脱数组和玩具代码,并尝试使用最佳实践和面向对象的原则来实现这一点,我知道我可以以一种更简单但可重复使用的方式来做到这一点.

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.

我的错误处理有问题.我对 draw() 代码的想法如下 -

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

1) 如果有另一张卡片,则返回它并移动迭代器.这将消除对弃牌堆的需要,因为弃牌将在迭代器后面,而 .last() 牌是刚刚抽出的牌.

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) 如果没有另一张卡片并且卡片"为空,则运行 emptyDeck() 方法.该方法将在子类中实现.例如,在纸牌游戏中,您可能希望在通过牌组 x 次后结束游戏,因此您可能不想再抽一张牌.

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) 如果牌组不是空的并且你没有更多的牌,那么你调用 endOfDeck() 方法,该方法将被子类化.同样,您可能想要洗牌或简单地重置迭代器

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();

}

推荐答案

当一个方法由于内部错误而无法返回有意义的结果时,该方法应该抛出异常而不是只返回一些东西.因此,当您的 draw 方法中发生该方法本身无法管理的错误时,它应该自己抛出一个异常,然后由调用者处理.

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.

在这种情况下,我将创建一个新的异常类 DeckEmptyException.当牌组为空时,draw 方法将抛出该异常而不是返回一张卡片.任何调用 draw 方法的人都必须处理这个异常.

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.

这篇关于如何处理返回对象和处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆