Minty
Minty

Reputation: 11

PHP Try-Catch Failing to Catch (Not a 'Warning')

I have some PHP code that should cause and catch two exceptions:

try{
    @$this->connector->connect(); // Suppress the default warning (doesn't effect 'throw')
} catch(BadArgumentException $e) {} // Works, no error, following code executes.

try{
    @$this->connector->connect(array('user' => 'Doesn\'t exist', 'pass' => 'invalid'));
} catch(AuthenticationException $e) {} // DOESN'T WORK - Passed to the exception handler.

echo 'Not executed!'; // This isn't executed.

I have tried generalising them to catch(Exception $e) but get the same problem... no idea why.

Any help?

Upvotes: 0

Views: 934

Answers (2)

Alex Weinstein
Alex Weinstein

Reputation: 9891

You should also know that using @ is EXTREMELY slow in PHP. Please, please, please don't use it in your production code.

Upvotes: 0

Minty
Minty

Reputation: 11

OK I found out it was a namespacing problem: it seems PHP doesn't complain when you try and use a non-existant namespaced element (in this case use Framework\AuthenticationException when really I needed use Framework\Connector\AuthenticationException). Everything's peachy now :)

Cheers

Upvotes: 1

Related Questions