Using exception or assertation

People tend to mix up the intended use of exceptions and assertations.

Assertations

Most assertation implementations will show up a form and most of them allow you to ignore the assertation. I once was using a program which even asked me whether I wanted to ignore that assertation in the future.

Using asserations as normal errors is wrong. Assertations aren’t meant to be errors, neither exceptions.

Using assertations (as you should) is to make sure that the conditions that should be right are right, if they aren’t right you will know and you know you made an error and not the program or the user.

A seemingly ridiculous example is the following assertation:

ASSERT(1 + 1 == 2)

If a programmer uses the assumption that 1 + 1 is 2 and isn’t sure about this he would use an assertation to be notified when he was wrong or missed something.

Exceptions

Most people treat exceptions as failures of a piece of program. And some programmers don’t tend to clean up stuff before they throw an exception for they just think that noone wants to do anything anymore, for an exception, they think, is fatal.

Exception aren’t fatal. Exceptions aren’t errors. Exceptions occur, for they are just as the word says exceptions. You just got to handle any exception that might occur. An exception becomes fatal when this isn’t handled for it is either totaly unexpected or just unhandable, where in both cases letting the exception stay unhandled is a better alternative than using an assertation.

4 thoughts on “Using exception or assertation”

  1. Well said. Python lives by this ‘exception =! error’ rule. It’s a common idiom called (i think): ‘ask forgiveness instead of permission’ as in:

    try:
    i = list[2]
    except IndexError:
    i = 0

    if the list doesnt contain the required item, it uses a default. no harm done.

  2. Python allows the use of exceptions as they are meant to be used.
    In most other languages exceptions are *very* slow and therefore there are usualy test functions which have to be used instead of catching exceptions which I think is a shame.

    For instance Int.Parse in .net throws an exception when it can’t be parsed.. throwing exceptions takes time.
    Int.TryParse doesn’t throw an exception when parsing fails but just returns false, so that one has to be used whether you like it or not.

  3. To be quite more exact. Assert ought to be used for debugging, assert values should be right, if they aren’t the program contains a bug and will fail. When finalise the program there should be no assert in the code. I thought.

  4. Assertations are used in debugging for they only should be called when there is some programatical error in the architecture while programming (programmer’s fault, not system or user’s or environment’s). But this doens’t limit them to debugging. In some cases where a programmer finds a certain assertation so important that he keeps it there.

Leave a Reply

Your email address will not be published. Required fields are marked *