Types of assert

This lesson explains why assert statements are needed and then explores different types of assert statements in D.

We'll cover the following

Need for assert #

In the previous chapter, we have seen how exceptions and scope statements are used toward program correctness. assert is another powerful tool to achieve the same goal. assert ensures that the certain assumptions that the program is based on are valid.

It may sometimes be difficult to decide whether to throw an exception or to call assert. I will use assert in all of the examples below without much justification. We will look at the differences later in the chapter.

Although not always obvious, programs are full of assumptions. For example, the following function is written under the assumption that both age parameters are greater than or equal to zero:

Get hands-on with 1200+ tech skills courses.