In the provided code snippet, what improvements can be made to handle exceptions and errors more effectively?

The provided code snippet does not handle exceptions or errors effectively. To improve error handling, we can use try-catch blocks to catch exceptions and handle them appropriately. This ensures that the code can gracefully handle errors without crashing the application.

try {
    $file = 'example.txt';
    $handle = fopen($file, 'r');
    if (!$handle) {
        throw new Exception("Cannot open file: $file");
    }
    
    // Read file contents
    $contents = fread($handle, filesize($file));
    fclose($handle);
    
    echo $contents;
} catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}