假设我有以下代码。
为了对此进行测试,我更改了服务器 IP 以模拟错误消息。下面的 IP 不存在,所以 Unhandled Exception
消息是:无法连接到 10.199.1.7。错误 113。没有到主机的路由
这会显示一个带有 PHP 代码的丑陋屏幕。是否可以捕获此错误?
try {
$ssh = new Net_SSH2('10.199.1.7');
if (!$ssh->login('deploy', $key)) {
throw new Exception("Failed login");
}
} catch (Exception $e) {
???
}
最佳答案
浏览图书馆。
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.
}
关于php - 如何使用 PHPseclib 捕获未处理的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13589093/