【问题标题】:How to catch un-handled errors with PHPseclib?如何使用 PHPseclib 捕获未处理的错误?
【发布时间】:2012-11-27 16:51:31
【问题描述】:

假设我有以下代码。

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

【问题讨论】:

    标签: php phpseclib


    【解决方案1】:

    浏览了图书馆。

    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.
    }
    

    【讨论】:

    • 很难弄清楚这一点。我将如何在上面的现有代码中实现这一点?
    • 所以基本上将boostrap.php文件包含在file.php中,它应该可以工作吗?
    • 很好地调用了 user_errors。当我看到这个问题并在 Github 上使用异常而不是 SourceForge 上的官方实现时,我去谷歌搜索。我的答案是 100% 错误,所以我将其标记为删除。感谢您的关注和 +1
    • Np,我也是先找到git hub版本的。
    • 正是我想要的。谢谢。
    【解决方案2】:

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

    【讨论】:

    • 使用@总是一个坏主意!
    • 这称为静默错误,这不是解决问题的方法,只能忽略它
    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 2019-06-30
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    相关资源
    最近更新 更多