It happens to all of us at one point or another.
I find that playing a spellcaster, the biggest issue I have is when I'm planning on doing something like a fireball, or other spell that deals out a lot of damage to whoever is in that area, inevitably one of my party members will rush up and get in the way so I have to think of an alternate plan.
I agree with PCat that SOMETHING is usually better than nothing. Depending on what sort of penalties your DM enforces for things like firing into melee etc. , no matter what, if you have something to throw, throw it, if you have a bow, fire it, if you are close enough to take a jab at the enemy with your sword, go for it. Even if you face a penalty, you never know, you could roll a 20.
Of course, in my game, firing into melee, we just have a -4 penalty. There is no chance of harming a party member. Some DMs would have it so that a party member gets hit if you miss.
Basically, what I do now is always have a few fail safe backup plans. If I can't cast a serious spell without hurting one of my group, I usually fire off my crossbow, even if it's at a -4. I'm pretty good with my crossbow, so even with the penalty there's a good chance I'll hit. Spellcasters often fallback on magic missile too. I know I've used that a few times when it seemed the best thing to do. Unless I really need to conserve spells.
Figure out what your characters safe fallback action is. Know it and know that if your other plans don't work out for whatever reason, you can always do that.