In the context of PHP and SSH connections, what are best practices for handling errors and troubleshooting connection issues?

When handling errors and troubleshooting SSH connection issues in PHP, it is important to use exception handling to catch and handle any potential errors that may occur during the connection process. Additionally, implementing proper error logging and debugging techniques can help identify the root cause of connection problems.

try {
    $connection = ssh2_connect('example.com', 22);
    if (!$connection) {
        throw new Exception('Unable to establish SSH connection');
    }
    
    $auth = ssh2_auth_password($connection, 'username', 'password');
    if (!$auth) {
        throw new Exception('Authentication failed');
    }
    
    // Continue with SSH operations
} catch (Exception $e) {
    error_log('SSH Connection Error: ' . $e->getMessage());
    // Handle the error or display a message to the user
}