previous | start | next

## Assertions

- Functions often contain implicit assumptions (denominators should be nonzero, salaries should not be negative).
- Such illegal values can creep into a program due to an input or processing error - with distressing regularity!
- Assertions provide a valuable sanity check.

  >     void raise_salary(Employee& e, double by)
  >     {
  >        assert(e.get_salary() >= 0 );
  >        assert(by >= -100);
  >        double new_salary = e.get_salary() * (1 + by / 100);
  >        e.set_salary(new_salary);
  >     }

- If an assertion is not satisfied, the program terminates with a useful error message showing the line number and the code of the failed assertion:

  >     assertion failed in file finclac.cpp line 61: by >= -100

previous | start | next