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

前端之家收集整理的这篇文章主要介绍了如何使用PHPseclib捕获未处理的错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有以下代码.

为了测试这一点,我更改了服务器IP以模仿错误消息.下面的IP不存在,因此Unhandled Exception消息为:无法连接到10.199.1.7.错误113.无主机路由

这会显示一个带有PHP代码的丑陋屏幕.是否有可能发现此错误

  1. try {
  2. $ssh = new Net_SSH2('10.199.1.7');
  3. if (!$ssh->login('deploy',$key)) {
  4. throw new Exception("Failed login");
  5. }
  6. } catch (Exception $e) {
  7. ???
  8. }
通过图书馆看.
  1. user_error('Connection closed by server',E_USER_NOTICE);

它会触发错误.您可以使用http://php.net/manual/en/function.set-error-handler.php处理这些错误

例如

  1. // Your file.PHP
  2. $ssh = new Net_SSH2('10.199.1.7');
  3. $ssh->login('deploy',$key);
  4.  
  5. // bootstrap.PHP
  6. // This will catch all user notice errors!!!
  7. set_error_handler ('errorHandler',E_USER_NOTICE)
  8.  
  9. function errorHandler($errno,$errstr,$errfile,$errline) {
  10. echo 'Error';
  11. // Whatever you want to do.
  12. }

猜你在找的PHP相关文章