java - How to deal with returning an object and handling errors -



java - How to deal with returning an object and handling errors -

thanks checking out question. have next code java deck. want step away arrays , toy code , seek using best practice , object oriented principles one, i'm aware can in much simpler, less re-usable, manner.

the end goal create cardgame framework can utilize deal mundane parts of deck management while concentrating on implementation of different games.

i'm having issue error handling. thought draw() code follows -

1) if there's card homecoming , move iterator along. eliminate need discard pile discards behind iterator .last() card beingness 1 drawn.

2) if there isn't card , "cards" empty run emptydeck() method. method implemented in subclasses. illustration in solitaire may want end game after running through deck x number of times may not want draw card more.

3) if deck isn't empty , have no more cards phone call endofdeck() method going subclassed. again, may want shuffle deck or reset iterator

however i'm getting old "must homecoming card" error message. i've tried creating custom exception can specify 1 handler method. can suggest smart way this?

public abstract class deck { private arraylist<card> cards; private listiterator<card> deckposition = cards.listiterator(); /** * */ public deck() { } public card draw() { card drawn; seek { if(deckposition.hasnext()) { drawn = deckposition.next(); } else if(cards.isempty()) { emptydeck(); } else { endofdeck(); } } catch(exception e) { system.out.println("exception when drawing card, check try/catch block in draw() method of deck"); e.printstacktrace(); } { homecoming drawn; } } public abstract void endofdeck(); public abstract void emptydeck(); }

when method unable homecoming meaningful result due internal error, method should throw exception instead of returning something. when error occurs in draw method method can not manage, should throw exception handled caller.

in case create new exception class deckemptyexception. when deck empty, draw method throw exception instead of returning card. whoever calls draw method have deal exception.

java exception-handling arraylist iterator return

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -