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:
“What is man in nature? A nothing in comparison with the infinite, an all in comparison with the nothinga mean between nothing and everything.”
—Blaise Pascal (16231662)
“[Girls] study under the paralyzing idea that their acquirements cannot be brought into practical use. They may subserve the purposes of promoting individual domestic pleasure and social enjoyment in conversation, but what are they in comparison with the grand stimulation of independence and self- reliance, of the capability of contributing to the comfort and happiness of those whom they love as their own souls?”
—Sarah M. Grimke (17921873)
“For my part I do, qua lay physicist, believe in physical objects and not in Homers gods; and I consider it a scientific error to believe otherwise.”
—Willard Van Orman Quine (b. 1908)
“Mothers risk alienating their mates if they expect them to hold or care for the baby exactly as they do. Fathers who are constantly criticized or corrected may lose interest in handling the baby, and this is a loss for everyone. The cycle is a dangerous one. Now the same mother feels bitter because she is no longer getting any help at home.”
—Cathy Rindner Tempelsman (20th century)