Jump to content

Catching exceptions


Recommended Posts

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');
    }
}
?>
Link to comment
Share on other sites

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.

:)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.