I also encountered this situation and, like you, I didn’t want countless if / else if / else if / else, as this makes the code less readable.
I ended the Exception class with my own. The following is an example class for validation problems that, when triggered, will give a less serious "log notification"
class ValidationEx extends Exception { public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } }
In my main code, I call it;
throw new ValidationEx('You maniac!');
Then at the end of the Try statement there is
catch(ValidationEx $e) { echo $e->getMessage(); } catch(Exception $e){ echo $e->getMessage(); }
Happy for comments and criticism, we are all here to learn!
source share