开发者

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

开发者 https://www.devze.com 2022-12-21 19:10 出处:网络
I have some PHP code that should cause and catch two exceptions: try{ @$this->connector->connect(); // Suppress the default warning (doesn\'t effect \'throw\')

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(Aut开发者_JAVA百科henticationException $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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消