How can PHP developers customize error messages for specific exceptions, such as integrity constraint violations, to provide more user-friendly feedback?

When handling specific exceptions like integrity constraint violations in PHP, developers can customize error messages to provide more user-friendly feedback by catching the exception and then displaying a custom error message to the user. This can help improve the user experience by providing clearer guidance on how to resolve the issue.

try {
    // Code that may throw an exception, such as an integrity constraint violation
} catch (PDOException $e) {
    if ($e->getCode() == '23000') {
        // Custom error message for integrity constraint violation
        echo "Sorry, this action cannot be completed due to a data constraint. Please check your input and try again.";
    } else {
        // Handle other types of exceptions
        echo "An error occurred. Please try again later.";
    }
}