views:

352

answers:

2

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?

+1  A: 

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

Minty
A: 

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

Alex
Could you provide any explanation as to why @ is slow and should be avoided?
pako
Google "php error suppression performance": http://www.phpdeveloping.co.za/error-handling/error-suppression-operator.html
Alex
Thanks for the advice :) This is only for testing though, I try and avoid error suppression in 'real' code.
Minty