NotionCommotion Posted May 18, 2017 Share Posted May 18, 2017 Why isn't CustomException caught, yet Exception is? <?php namespace Notion\RestApp; class CustomException 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"; } public function customFunction() { echo "A custom function for this type of exception\n"; } } ?> <?php namespace Notion\RestApp; class foo { protected function bar() { try { $this->error(); } catch(\CustomException $e) {/* Suppress CustomException errors */} //catch(\Exception $e) {/* This will catch CustomException */} $this->next(); } protected function error() { throw new CustomException('error'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/303957-catching-exceptions/ Share on other sites More sharing options...
Solution kicken Posted May 18, 2017 Solution Share Posted May 18, 2017 Because \CustomException does not exist. \Notion\RestApp\CustomException does. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303957-catching-exceptions/#findComment-1546596 Share on other sites More sharing options...
requinix Posted May 18, 2017 Share Posted May 18, 2017 If your next question is "why doesn't PHP complain that \CustomException does not exist?" then the answer is "because PHP doesn't need to check that". Think about why not. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303957-catching-exceptions/#findComment-1546597 Share on other sites More sharing options...
NotionCommotion Posted May 18, 2017 Author Share Posted May 18, 2017 Because \CustomException does not exist. \Notion\RestApp\CustomException does. Because \CustomException does not exist. \Notion\RestApp\CustomException does. Must have been hungover. If your next question is "why doesn't PHP complain that \CustomException does not exist?" then the answer is "because PHP doesn't need to check that". Think about why not. Quote Link to comment https://forums.phpfreaks.com/topic/303957-catching-exceptions/#findComment-1546601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.