I look at it narratively.
Is there anything to suggest a caster becomes incapable of using their magic and casting a spell if they target something "invalid"? Like all of a sudden they can't get the words out, their hand motions go all squiggly and they can't seem to grab the requisite components? I suspect very few people would say they agree with that interpretation.
Instead, what is more likely is that the caster goes through the whole process of casting the spell, but there is just no result from it because the spell doesn't affect the target. "You can't charm a doorknob" as was said. This is what it probably looks like if we take the whole scene from a narrative point of view.
So you can cast a spell onto most anything or out into the ether... but it only has a perceptible effect if the spell has a target to interactive with. And if it just so happens that the target can actually be a valid target (even if you weren't aware it was when you started), then lucky you!