How can error handling be improved when checking server status using PHP?

When checking server status using PHP, error handling can be improved by implementing try-catch blocks to catch any potential exceptions that may occur during the server status check. This will help in gracefully handling errors and providing appropriate feedback to the user.

try {
    $serverStatus = file_get_contents('http://example.com/server-status');
    
    if($serverStatus === false) {
        throw new Exception('Failed to retrieve server status.');
    }
    
    // Process server status data here
    
} catch(Exception $e) {
    echo 'Error: ' . $e->getMessage();
}