PHP:没有被try ... catch捕获的异常

时间:2012-12-17 22:40:07

标签: php exception runtime-error

我目前正在为我的一个项目开发自动加载器类。下面是控制器库的代码:

     public static function includeFileContainingClass($classname) {

        $classname_rectified = str_replace(__NAMESPACE__.'\\', '', $classname);
        $controller_path     = ENVIRONMENT_DIRECTROY_CONTROLLERS.strtolower($classname_rectified).'.controller.php';

        if (file_exists($controller_path)) {

           include $controller_path;
           return true;

        } else {

           // TODO: Implement gettext('MSG_FILE_CONTROLLER_NOTFOUND')
           throw new Exception('File '.strtolower($classname_rectified).'.controller.php not found.');
           return false;

        }

     }

这是我尝试调用自动加载器的文件的代码:

  try {

     spl_autoload_register(__NAMESPACE__.'\\Controller::includeFileContainingClass');

  } catch (Exception $malfunction) {

     die($malfunction->getMessage());

  }

  // TESTING ONLY
  $test = new Testing();

当我尝试强制发生故障时,我收到以下消息:

Fatal error: Uncaught exception 'Exception' with message 'File testing.controller.php not found.' in D:\cerophine-0.0.1-alpha1\application\libraries\controller.library.php:51 Stack trace: #0 [internal function]: application\Controller::includeFileContainingClass('application\Tes...') #1 D:\cerophine-0.0.1-alpha1\index.php(58): spl_autoload_call('application\Tes...') #2 {main} thrown in D:\cerophine-0.0.1-alpha1\application\libraries\controller.library.php on line 51

什么似乎错了?

1 个答案:

答案 0 :(得分:2)

因为你没有抓住它......

spl_autoload_register(__NAMESPACE__.'\\Controller::includeFileContainingClass');

try {
    $test = new Testing();
} catch (Exception $malfunction) {
    die($malfunction->getMessage());
}