user1911318
user1911318

Reputation:

PHP: Exception not caught by try ... catch

I currently am working on an autoloader class for one of my projects. Below is the code for the controller library:

     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;

        }

     }

And here's the code of the file I try to invoke the autoloader on:

  try {

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

  } catch (Exception $malfunction) {

     die($malfunction->getMessage());

  }

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

When I try to force a malfunction, I get the following message:

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

What seems to be wrong?

Upvotes: 3

Views: 289

Answers (1)

Glavić
Glavić

Reputation: 43552

Because you are not catching it...

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

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

Upvotes: 2

Related Questions