IIRC, one of the key elements of intentional grounding is that there is no receiver in the vicinity. This applies with or without the ball being thrown out of bounds.
If there is a receiver, you may throw out of bounds, in fact, that’s the safest possible way to throw a ball away. If there is no receiver, then even throwing out of bounds won’t prevent an intentional-grounding flag from being thrown on you.
If the QB throws them deliberately at a lineman, sure. The intent is the operative part.
If a QB is hit as he throws and the ball ends up hitting a lineman, I wouldn’t expect to see a flag thrown as the trajectory was clearly unintentional.