在扩展异常上尝试/捕获失败

时间:2013-12-27 20:34:46

标签: php exception error-handling

有一点令人沮丧的尝试/捕获错误,我想知道我是否只是缺少一些东西。任何/所有输入非常感谢。我在Exception类上有几个扩展,似乎经过第二个扩展,catch不再有效。

IE PublicException扩展Exception / FileNotFoundException扩展PublicException;当我抛出一个FileNotFoundException时,它不会被一个PublicException catch块捕获,它应该是它的扩展名。

我有一个遵循这种模式的框架路由器

try {

    // process page routes
    // It is in this stage that one of my page controllers throws 
    // the FileNotFoundException
    if (!$result) {
        // Handle if the page request is not found
    }
} catch (HttpException $e) {
    // process
} catch (PublicException $e) { <--- This is failing to be caught
    // process public exception
} catch (Exception $e) {
    // Process Exception
}

然后我有:

class PublicException extends Exception {
}

class FileNotFoundException extends PublicException {
    public function __construct($msg, $code = null, Exception $prev = null) {
        // Custom Exception Handling
    }
}

当我抛出一个新的FileNotFoundException时,catch(PublicException $ e)实际上并没有捕获异常,当然它应该是,因为FileNotFound是PublicException的扩展。如果我在我的错误块中抛出一个PublicException,它就会按预期工作。

我错过了什么?这在PHP世界中并不是什么新东西!感谢您的建议,并为长篇文章感到抱歉,试着详细说明。

0 个答案:

没有答案
相关问题