Jump to content

Catching exceptions


Go to solution Solved by kicken,

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
https://forums.phpfreaks.com/topic/303957-catching-exceptions/
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
https://forums.phpfreaks.com/topic/303957-catching-exceptions/#findComment-1546601
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.