How to catch errors without processing using PHPseclib?

Let's say I have the following code snippet.

To verify this, I change the IP address of the server to simulate error messages. The IP below does not exist, therefore the message Unhandled Exception : Cannot connect to 10.199.1.7. Error 113. No route to host Cannot connect to 10.199.1.7. Error 113. No route to host

Displays an ugly screen with PHP code. Can this error be caught?

 try { $ssh = new Net_SSH2('10.199.1.7'); if (!$ssh->login('deploy', $key)) { throw new Exception("Failed login"); } } catch (Exception $e) { ??? } 
+6
source share
2 answers

I looked through the library.

 user_error('Connection closed by server', E_USER_NOTICE); 

It causes errors. You can handle these errors using http://php.net/manual/en/function.set-error-handler.php

eg.

 // 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. } 
+11
source

You can use @ before calling the function. @operator

0
source

All Articles