7

次のコードがあるとしましょう。

これをテストするために、サーバー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) {
     ???
}
4

2 に答える 2

13

図書館を調べた。

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.
}
于 2012-11-27T17:08:50.830 に答える
0

関数呼び出しの前に @ を使用できます。@ 演算子

于 2012-11-27T16:55:35.900 に答える