Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Assertions

Part of TutorialFundamentals

Description

Using assert is a form of testing the code that you write. An assert statement takes an expression as it's first argument, and if that expression evaluates to true, then nothing happens. If however the expression evaluates to false, the statement will raise an AssertError and will print out an error message plus an additional string if it's provided as a second argument.

Example

import std.stdio;

void main() 
{
    assert(221 % 5 == 1);
    assert(25 * (4 * 20) + 4 == 2004);
    
    assert(2 == 4, "2 is not equal to 4!");
}

More Information

More info can be found in the D Specification.