Thursday, November 25, 2010

Assertions

Assertions are methods to verify the correctness of a program. They are conditions, which throw an exception when not met.
Assertions mostly preceed sections of the code, which do not work without meeting the condition. If an assertion is thrown, the programmer knows, that he has to catch the exception in this area.
The expression System.Diagnostics.Debug.Assert() can be used in the debug configuration, System.Diagnostics.Trace.Assert() in the release version. As example a function for division, which prohibits a division through 0:

public double divide(double divident, double divisor)
{
    System.Diagnostics.Debug.Assert(divisor != 0);
    return (divident / divisor);
}

No comments:

Post a Comment