8

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.

10
  • 7
    This just sounds broken. Commented Nov 21, 2010 at 19:31
  • 1
    I'm from a Java background :) Commented Nov 21, 2010 at 19:33
  • 1
    Pretty hard to diagnose without any code to look at. Commented Nov 21, 2010 at 19:34
  • 4
    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? Commented Nov 21, 2010 at 19:55
  • 1
    @Zdnil: Err... no. Good java code doesn't use exceptions for things like this either. Exceptions are for errors which should not be handled by your immediate caller. (i.e. database failure), not for routine checks (player can't move there). Exception s are for Exception al cases. Commented Nov 21, 2010 at 20:03

3 Answers 3

6

I agree with some of the comments that this is an odd way to achieve what you want to do, however I can't see any actual code problems. My test case:

class TestException extends Exception { } function raiseTestException() { try { throw new TestException("Test Exception raised"); } catch(TestException $e) { throw $e; } return; } try { raiseTestException(); } catch(TestException $e) { echo "Error: " . $e->getMessage(); } // result: "Error: Test Exception raised"; 

Is it actually the RoundAlreadyStartedException that isn't being caught, or some other error?

EDIT: wrapped in classes, on the off chance that it makes a difference (it doesn't):

class TestException extends Exception { } class TestClass { function raiseTestException() { try { throw new TestException("Test Exception raised"); } catch(TestException $e) { throw $e; } return; } } class CallerClass { function callTestCallMethod() { $test = new TestClass(); try { $test->raiseTestException(); } catch(TestException $e) { echo "Error: " . $e->getMessage(); } } } $caller = new CallerClass(); $caller->callTestCallMethod(); // result: "Error: Test Exception raised"; 
Sign up to request clarification or add additional context in comments.

Comments

4

I had this same strange behaviour with exceptions not bubbling. It turns out that I had the exceptions in another namespace but I wasn't explicitly using it and PHP didn't complain about that. So, adding using mynamespace\exceptions\MyException; solved it. Maybe that could be happening to you too.

HTH.

1 Comment

Additionally, if you are using namespaces in the class where you are trying to catch things, make sure to do: ... } catch (\Exception $e) { ... (Note the backslash, indicating it's the "root namespace")
2

I just this same problem in Laravel 4.2

As Laravel listens to the \Exception handler you can't use the root to throw an Exception and expect to bubble though the application unless you modify or extend Laravel. What you need is to create an empty Exception which extends \Exception for your namespace and then catch your namespaced Exception how ever you need to.

<?php namespace Yournamespacehere; class Exception extends \Exception { } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.