How can error handling be improved when executing SQL queries within PHP functions to ensure smoother execution and debugging processes?

When executing SQL queries within PHP functions, error handling can be improved by utilizing try-catch blocks to catch any exceptions that may occur during query execution. This allows for smoother execution and better debugging processes as any errors can be properly handled and logged.

try {
    // Your SQL query execution code here
    $result = $pdo->query("SELECT * FROM table");
} catch (PDOException $e) {
    // Handle the exception, log the error, or display a custom error message
    echo "Error executing SQL query: " . $e->getMessage();
}