How can errors related to file operations be better managed and handled in PHP scripts?

Errors related to file operations in PHP scripts can be better managed and handled by using error handling functions such as `try`, `catch`, and `finally` blocks to capture and handle exceptions that may occur during file operations. This allows for more graceful error handling and prevents scripts from crashing unexpectedly.

try {
    $file = fopen("example.txt", "r");
    if (!$file) {
        throw new Exception("Unable to open file.");
    }

    // Perform file operations here

} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
} finally {
    if ($file) {
        fclose($file);
    }
}