如何使用PHPseclib捕获未处理的错误?

luc*_*axi 7 php 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) {
     ???
}
Run Code Online (Sandbox Code Playgroud)

E_p*_*E_p 13

通过图书馆看.

user_error('Connection closed by server', E_USER_NOTICE);
Run Code Online (Sandbox Code Playgroud)

它会触发错误.您可以使用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.
}
Run Code Online (Sandbox Code Playgroud)