假设我有以下代码.
为了测试这一点,我更改了服务器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.
}