How can developers effectively handle errors and exceptions when executing SQL statements in PHP?

When executing SQL statements in PHP, developers can effectively handle errors and exceptions by using try-catch blocks to catch any potential exceptions thrown by the database operations. Within the catch block, developers can log the error message or handle it appropriately to prevent crashing the application.

try {
    // Execute SQL statement here
} catch (PDOException $e) {
    // Log or handle the exception
    echo "Error: " . $e->getMessage();
}