Assertion (computing) - Comparison With Error Handling

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:

    Clay answered the petition by declaring that while he looked on the institution of slavery as an evil, it was ‘nothing in comparison with the far greater evil which would inevitably flow from a sudden and indiscriminate emancipation.’
    State of Indiana, U.S. public relief program (1935-1943)

    The difference between human vision and the image perceived by the faceted eye of an insect may be compared with the difference between a half-tone block made with the very finest screen and the corresponding picture as represented by the very coarse screening used in common newspaper pictorial reproduction. The same comparison holds good between the way Gogol saw things and the way average readers and average writers see things.
    Vladimir Nabokov (1899–1977)

    Knowledge, like matter, [my father] would affirm, was divisible in infinitum;Mthat the grains and scruples were as much a part of it, as the gravitation of the whole world.—In a word, he would say, error was error,—no matter where it fell,—whether in a fraction,—or a pound,—’twas alike fatal to truth.
    Laurence Sterne (1713–1768)

    For a novel addressed by a man to men and women of full age; which attempts to deal unaffectedly with the fret and fever, derision and disaster, that may press in the wake of the strongest passion known to humanity; to tell, without a mincing of words, of a deadly war waged between flesh and spirit; and to point the tragedy of unfulfilled aims, I am not aware that there is anything in the handling to which exception can be taken.
    Thomas Hardy (1840–1928)