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

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

问题描述

感谢检查出我的问题。我有以下的code为Java平台。我想辞去阵列和玩具code闯一闯使用最佳实践和面向对象的原则,为这一个,我知道我可以在一个更简单,但不太可重复使用的,地做到这一点。

我们的最终目标是建立一个框架,cardgame,我可以用它来对付甲板管理中的世俗方面,而在不同的游戏实施集中。

我有我的错误处理的问题。我对平局()code思路如下 -

1)如果有另一张卡归还和待着迭代器。这将消除对弃牌堆的需要,因为丢弃将会同。去年()卡是刚刚得出的一个迭代器的后面。

2)如果没有其他牌和卡是空的运行emptyDeck()方法。这种方法将在子类中实现。例如,在纸牌你可能要结束通过倍甲板x次后运行游戏,所以你可能不希望抓一张牌了。

3)如果甲板不是空的,你有没有更多的卡,然后调用它是要被继承的endOfDeck()方法。同样,你可能要洗牌或者干脆复位迭代器

不过我越来越老必须返回一个卡的错误消息。我试图创建一个自定义异常,但我只能指定一个处理程序方法。任何人都可以提出一个聪明的办法做到这一点?

 公共抽象类甲板
{私人的ArrayList<卡>牌;
私人的ListIterator<卡> deckPosition = cards.listIterator();
/ **
 *
 * /
公共甲板()
{
}市民卡抽奖()
{
    卡绘制;    尝试
    {
        如果(deckPosition.hasNext())
        {
            绘制= deckPosition.next();
        }
        否则如果(cards.isEmpty())
        {
            emptyDeck();
        }
        其他
        {
            endOfDeck();
        }
    }    赶上(例外五)
    {
        的System.out.println(异常绘图卡时,请检查try / catch块中的draw()甲板的方法);
        e.printStackTrace();
    }    最后
    {
        返回绘制;
    }
}公共抽象无效endOfDeck();
公共抽象无效emptyDeck();}


解决方案

当一个方法无法返回有意义的结果,由于内部错误,该方法应该抛出一个异常,而不是返回只是一些的。因此,当发生在你的拉伸方法的错误其中该方法本身不能管理,应该抛出异常本身,然后由主叫处理。

在这种情况下,我将创建一个新的异常类 DeckEmptyException 。当甲板上是空的,抽签的方法将抛出而不是返回卡例外。那么谁调用Draw方法将不得不处理这个异常。

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

}

解决方案

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.

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

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