7

假设我有以下代码。

为了测试这一点,我更改了服务器 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) {
     ???
}
4

2 回答 2

13

翻阅图书馆。

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.
}
于 2012-11-27T17:08:50.830 回答
0

您可以在函数调用前使用 @ 。@ 操作员

于 2012-11-27T16:55:35.900 回答