Ok - I am confused as to exactly which question you are asking here
1. shooting into melee
2. firing a ranged weapon while IN melee
3. throwing the melee weapon AT someting, while in or out of melee combat.
Q1 = short answer - see pg 124, top left hand corner, first printing PHB = -4 penalty to the shot to avoid hitting your ally, unless you have the precise shot feat.
Q2 = yes, you can do this, but it provokes an AoO.
Q3 = see irregular missiles advice (pg number is lost at the moment sorry)
Advanced answer to Q1 -
The reason for this -4 penalty is that you are presumed to be fighting another "similar sized" opponent - and that someone else - friend or foe standing five feet in front of you gives you "one half cover" against attacks from that direction.
Of course, if your buddy is a gnome fighter engaged hand to hand with an ogre - the "cover" factor is dramatically reduced for you - and dramatically increased for the ogre's allies trying to shoot your "little" buddy.
If you want to deter this tactic, you should remind your players that if they miss, they will have to check to see the if the "cover" - in this case your ally - is struck. The rules for cover can be found on pg 132 of the PHB.