There are many varied reasons why we throw up, many of which every one of us will encounter at some point in our lives. Everything from extremes of pain or shock, to ingesting too much alcohol or toxic chemicals than or bodies can handle can cause us to vomit. Although of course the most common causes are either being drunk for some people, or mild stomach infections, which usually last only a couple of days.
From the body's perspective the reason that we throw up is to try to get something harmful out of our system. Because obviously most of the chemicals in our body are there as a result of us having eaten something containing them. And usually vomiting although unpleasant is the best way to rid the body of unwanted substances. For example after vomiting when we have had too much alcohol, it will often make us feel instantly sober and a lot better. The same as if we were to eat something that was mouldy or otherwise harmful to us. As soon as it has been expelled from the body we are better again.
It isn't always the case however that we instantly feel better after vomiting has occurred though. For example if someone is subjected to extreme pain suddenly, such as dislocating something, then they will often vomit. Although this doesn't in any way help the situation, and in fact makes them feel even worse. This is also seemingly the case when we see or smell something shocking. For example the smell of rotten human flesh often makes medical students sick the first time they see and smell it.
From an evolutionary standpoint, one theory is that injury, and subsequent vomiting as a response is to try to save us from predators. This is because millions of years ago injuries would most commonly have been because of predators rather then accidents as they are today. And so emptying the contents of our stomachs all over ourselves not only made us smell and taste bad, but also made it easier for us to run away faster as we weren't holding any extra weight in our stomachs.
Exercising too hard or taking a blow to the stomach after a meal are also causes of vomiting, and in effect are the same thing. Although they might not feel the same, the cumulative effects of vigorous exercise is enough to make the abdominal muscles push against the stomach, causing reverse peristalsis. Which in effect is the same motion of swallowing and digestion, only backwards.
There are also certain drugs that can cause vomiting as a side effect for various reasons, although these tend to be the kind of drugs that would usually be accompanied by being in hospital as well. And so in those cases the vomiting is usually the least of your concerns. And similarly vomiting regularly is a sign of many different underlying medical disorders as well. Such as several cancers, stomach ulcers, food intolerances and many more.
So although throwing up might not be the best experience in the world, it is an essential one in terms of keeping us healthy and well. It is a case of our bodies being cruel to be kind, as the old saying goes, and is an effective tool in fighting infections.