How can error handling be improved in the PHP script to provide more detailed feedback on database operations?

Currently, the PHP script may only output generic error messages when a database operation fails, making it difficult to pinpoint the exact issue. To improve error handling, we can utilize PHP's try-catch block to catch exceptions thrown by database operations and provide more detailed feedback to the user.

try {
    // Perform database operation
    $result = $pdo->query("SELECT * FROM table");
    
    if(!$result) {
        throw new Exception("Error executing query");
    }
    
    // Process the result
    foreach($result as $row) {
        // Do something with the data
    }
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
}