2012-11-27 20 views
6

假设我有以下一段代码。如何用PHPseclib捕获未处理的错误?

要测试这个,我更改服务器IP以模拟错误消息。下面的IP不存在,所以Unhandled Exception消息是:Cannot connect to 10.199.1.7. Error 113. No route to host

这显示一个丑陋的屏幕与PHP代码。是否有可能发现这个错误?

try { 
     $ssh = new Net_SSH2('10.199.1.7');   
    if (!$ssh->login('deploy', $key)) { 
     throw new Exception("Failed login"); 
    } 
} catch (Exception $e) { 
    ??? 
} 

回答

11

翻阅资料库。

user_error('Connection closed by server', E_USER_NOTICE); 

它触发的错误。您可以使用http://php.net/manual/en/function.set-error-handler.php

// Your file.php 
$ssh = new Net_SSH2('10.199.1.7');   
$ssh->login('deploy', $key); 

// bootstrap.php 
// This will catch all user notice errors!!! 
set_error_handler ('errorHandler', E_USER_NOTICE) 

function errorHandler($errno, $errstr, $errfile, $errline) { 
    echo 'Error'; 
    // Whatever you want to do. 
} 
+0

难以解决这个问题。我将如何去实现这个到我现有的代码上面? – luckytaxi

+0

已更新为包含实施。 –

+0

所以基本上在'file.php'里面包含'boostrap.php'文件,它应该工作? – luckytaxi

0

你可以在你前面使用@函数调用。 @ operator

+4

使用@总是一个坏主意! –

相关问题