Those bright colors of many stinging caterpillars (and other nasty
tasting and stinging critters - and plants) is generally thought to be literally
an advertisement that says "come and taste me". This sounds totally
illogical when taken in the context of just one caterpillar, but when you
realize that when tried once the animal who got the sting, bad taste, or
whatever is going to remember and probably not repeat the mistake. So,
most of the nasties do survive at the expense of only a relatively few
sacrifices from the population.
Other species often take advantage of this and mimic coloration of the bad
ones. They have no defense, but get the advantage of the learned
response of the predator to the color pattern as well.
Just as an interesting side issue. If good tasting critters with the
advertising coloration outnumber the bad tasting ones for a time, the advantage
is lost. The predators will often actually learn to hunt for
that coloration to find good food. It's sort of a checks and balances
system though, as it will switch back once enough predators start getting nasty
Classic examples in North America are The Queen and Monarch butterflies,
which taste bad and are mimicked by the unrelated Viceroy. Also the
Pipevine Swallowtail, which is mimicked by numerous other species of butterflies
(Red-spotted Purple, female Tiger Swallowtail, female Diana Fritillary,
Spicebush Swallowtail, female Black Swallowtail, etc.). There are hundreds
of other examples.