Comparison With Error Handling
It is worth distinguishing assertions from routine error-handling. Assertions should be used to document logically impossible situations and discover programming errors — if the impossible occurs, then something fundamental is clearly wrong. This is distinct from error handling: most error conditions are possible, although some may be extremely unlikely to occur in practice. Using assertions as a general-purpose error handling mechanism is unwise: assertions do not allow for recovery from errors; an assertion failure will normally halt the program's execution abruptly. Assertions also do not display a user-friendly error message.
Consider the following example of using an assertion to handle an error:
int *ptr = malloc(sizeof(int) * 10); assert(ptr); // use ptr ...Here, the programmer is aware that malloc
will return a NULL
pointer if memory is not allocated. This is possible: the operating system does not guarantee that every call to malloc
will succeed. If an out of memory error occurs the program will immediately abort. Without the assertion, the program would continue running until ptr was dereferenced, and possibly longer, depending on the specific hardware being used. So long as assertions are not disabled, an immediate exit is assured. But if a graceful failure is desired, the program has to handle the failure. For example, a server may have multiple clients, or may hold resources that will not be released cleanly, or it may have uncommitted changes to write to a datastore. In such cases it is better to fail a single transaction than to abort abruptly.
Read more about this topic: Assertion (computing)
Famous quotes containing the words comparison with, comparison, error and/or handling:
“From top to bottom of the ladder, greed is aroused without knowing where to find ultimate foothold. Nothing can calm it, since its goal is far beyond all it can attain. Reality seems valueless by comparison with the dreams of fevered imaginations; reality is therefore abandoned.”
—Emile Durkheim (18581917)
“I have travelled a good deal in Concord; and everywhere, in shops, and offices, and fields, the inhabitants have appeared to me to be doing penance in a thousand remarkable ways.... The twelve labors of Hercules were trifling in comparison with those which my neighbors have undertaken; for they were only twelve, and had an end; but I could never see that these men slew or captured any monster or finished any labor.”
—Henry David Thoreau (18171862)
“Error is a supposition that pleasure and pain, that intelligence, substance, life, are existent in matter. Error is neither Mind nor one of Minds faculties. Error is the contradiction of Truth. Error is a belief without understanding. Error is unreal because untrue. It is that which seemeth to be and is not. If error were true, its truth would be error, and we should have a self-evident absurditynamely, erroneous truth. Thus we should continue to lose the standard of Truth.”
—Mary Baker Eddy (18211910)
“Madam, a circulating library in a town is as an evergreen tree of diabolical knowledge; it blossoms through the year. And depend on it ... that they who are so fond of handling the leaves, will long for the fruit at last.”
—Richard Brinsley Sheridan (17511816)