KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am often in a situation where I have a method where something can go wrong but an exception would not be right to use because it is not exceptional. For example: I am designing a monopoly game. The class Bank has a method buyHouse and a field which counts the number of houses left(there is 32 houses in monopoly). Something that could go wrong is a player buying a house when there is 0 left. How should I handle this. Here is 3 approaches I can come up with. 1. public void buyHouse(Player player, PropertyValue propertyValue) { if(houseCount < 0) throw new someException; .... //Not really an exceptional situation } 2. public boolean buyHouse(Player player, PropertyValue propertyValue) { if(houseCount < 0) return false; .... //This I think is the most normal approach but changing something //and returning if it was a success seems bad practice to me. } 3. public boolean housesLeft() { if(houseCount > 0) return true; return false; //Introducing a new method. But now I expect the client to call this method //first before calling buyHouse(). } What would you do?
Tags (comma-separated)
Save Edits
Cancel