No Duplicate Primary Key Error Message shown

please help me.
When i add a new Row, and i put something into the Primary Key Field that already exists, in my error.log
it throws an exception but on the website it still says “Row added Successfully” (its in german)

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["addRow"])) {
    $tableName = isset($_POST["tableName"]) ? trim($_POST["tableName"]) : '';
    $columnsResult = $conn->query("SHOW COLUMNS FROM $tableName");
    $columns = [];
    $valuesArray = [];
    while ($column = $columnsResult->fetch_assoc()) {
        $columns[] = $column['Field'];
        $valuesArray[] = isset($_POST[$column['Field']]) ? trim($_POST[$column['Field']]) : '';
    }
    $columnsString = implode(", ", $columns);
    $placeholders = implode(", ", array_fill(0, count($columns), "?"));
    $insertQuery = "INSERT INTO $tableName ($columnsString) VALUES ($placeholders)";
    $stmt = $conn->prepare($insertQuery);
    $types = str_repeat("s", count($valuesArray));
    $stmt->bind_param($types, ...$valuesArray);

    try {
        $stmt->execute();
        $successMessage = 'Neue Zeile erfolgreich hinzugefügt!';
    } catch (mysqli_sql_exception $e) {
        if ($e->getCode() == 1062) { // Duplicate entry error code
            $errorMessage = 'Fehler beim Hinzufügen der Zeile: Eintrag mit demselben Primärschlüssel existiert bereits.';
        } else {
            $errorMessage = 'Fehler beim Hinzufügen der Zeile: ' . $e->getMessage();
        }
        error_log($e->getMessage()); // Log the error message
    } finally {
        $stmt->close();
    }
}

Already shown in the Code waht i tried, catch mysqli sql exception