Here's the formulation of the linear program and the Gurobi code to solve it:

**Decision Variables:**

* `x`: Amount invested in toothpaste company A (in dollars)
* `y`: Amount invested in toothpaste company B (in dollars)

**Objective Function:**

Maximize profit:  `0.12x + 0.14y`

**Constraints:**

* **Total Investment:** `x + y <= 5000`
* **Minimum Investment in A:** `x >= 0.4 * (x + y)`  (At least 40% in A)
* **Minimum Investment in B:** `y >= 1000`
* **Non-negativity:** `x >= 0`, `y >= 0`


```python
import gurobipy as gp
from gurobipy import GRB

try:
    # Create a new model
    m = gp.Model("toothpaste_investment")

    # Create variables
    x = m.addVar(name="x") # Investment in company A
    y = m.addVar(name="y") # Investment in company B

    # Set objective function
    m.setObjective(0.12*x + 0.14*y, GRB.MAXIMIZE)

    # Add constraints
    m.addConstr(x + y <= 5000, "Total_investment")
    m.addConstr(x >= 0.4 * (x + y), "Min_investment_A")
    m.addConstr(y >= 1000, "Min_investment_B")
    m.addConstr(x >=0)
    m.addConstr(y >=0)


    # Optimize model
    m.optimize()

    if m.status == GRB.OPTIMAL:
        print(f"Optimal investment in Company A: ${x.x:.2f}")
        print(f"Optimal investment in Company B: ${y.x:.2f}")
        print(f"Maximum profit: ${m.objVal:.2f}")
    elif m.status == GRB.INFEASIBLE:
        print("The model is infeasible.")
    else:
        print(f"Optimization terminated with status {m.status}")


except gp.GurobiError as e:
    print(f"Error code {e.errno}: {e}")

except AttributeError:
    print('Encountered an attribute error')
```
