Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReflectionException unable to be caught?

I have this code:

namespace Some\Different\Name;
        try {
            $reflect  = new ReflectionClass($class);
X:          $instance = $reflect->newInstanceArgs($args);
        } catch (ReflectionException $e) {
            exit($e->getMessage());
        }

and I'm testing it trying to make a ReflectionException be thrown. And it gives me:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class MyClass does not have a constructor, so you cannot pass any constructor arguments' in ... on line X.

What am I doing wrong?

PS: I know why the exception is thrown, I just wanna know why it's not caught!

like image 568
Shoe Avatar asked Feb 25 '12 02:02

Shoe


1 Answers

Finally. It was a namespace problem. It's strange by the way that PHP doesn't notify that you are trying to catch an exception of a type (ReflectionException) that doesn't exists in the current namespace.

Just adding the \ to \ReflectionException helped me out because now it's able to find what type of exception I'm actually looking for.

Another solution would be to add:

use \ReflectionException;

just after the namespace declaration.

like image 58
Shoe Avatar answered Oct 30 '22 19:10

Shoe