I'm making a simple card game in PHP. When a user tries to play a card, I want to throw an exception if they can/can't. Rather than returning a number with a specific meaning (e.g. 1 means bad card, 2 means not your turn...etc.), I wanted to use customized exceptions. I'd catch these exceptions and display the message to the user.
I realize that exceptions are meant for out of the ordinary errors, but I think this is a good way to design my program.
The problem: my exceptions are being uncaught. I have a page called play.php which controls a class called Game, which has a Round that throws the exception. The play.php page gets the round from game, and makes function calls on it. However, it says the exception is not caught in round.
Is there a quick fix for this? How can I bubble up the exception from my Round class to my play.php page?
// in PLAY.php try { $game->round->startRound($game->players); } catch (RoundAlreadyStartedException $e) { echo $e->getMessage(); } // in ROUND class if (!($this->plays % (self::PLAYS_PER_ROUND + $this->dealer))) { try { throw new RoundAlreadyStartedException(); } catch (RoundAlreadyStartedException $e) { throw $e; } return; } I've tried catching, not catching, throwing, rethrowing, etc.
I realize that exceptions are meant for out of the ordinary errors, but I think this is a good way to design my program.I think I disagree with you ;). What's wrong with plain ol' return values?