Caterpillars are the larvae of butterflies and moths, which belong to the order Lepidoptera. Sawfly larvae look similar to caterpillars, but are an entirely different kind of insect. Sawflies are related to bees and wasps, and belong to the order Hymenoptera. Like caterpillars, sawfly larvae usually feed on plant foliage.
How can you tell the difference between a sawfly larva and a caterpillar? Count the prolegs. Caterpillars may have up to five pairs of abdominal prolegs (see parts of a caterpillar diagram), but never have more than five pairs. Sawfly larvae will have six or more pairs of abdominal prolegs*. Another notable difference, though it requires a closer look, is that caterpillars have tiny hooks called crochets, on the ends of their prolegs. Sawflies don't.
Another, less obvious difference between caterpillars and sawfly larvae is the number of eyes. Caterpillars almost always have 12 stemmata, 6 on each side of the head. Sawfly larvae usually have just a single pair of stemmata.
This distinction between sawflies and caterpillars is of particular importance if you are trying to rid your garden of a plant-eating caterpillar pest. Some pesticides (such as the bacteria Bacillus thuringiensis) work only on Lepidopteran larvae, and will not affect sawfly larvae. Before you apply any pesticide for a caterpillar problem, be sure to count the prolegs and identify your pest correctly.
* There are, of course, exceptions to every rule. Caterpillars of the family Megalopygidae, the flannel moths, are unusual in having 7 pairs of prolegs (2 more pairs than any other Lepidopteran larvae). Some sawfly larvae are stem borers or leaf miners; these larvae may have no prolegs at all.