Skip to content
All library documents

Using MQL5 Assertions to Catch Programming Errors During Development

Article MQL5 articles

Summary

The article explains assertions as checks for assumptions that should always hold during normal program execution. In MQL5, a macro can test a condition and report its expression, source file, function, line number, and an optional message. A soft version alerts and continues, while a hard version deliberately triggers a runtime failure to stop execution. Conditional compilation can remove assertion checks from the release build.

Examples cover checking input and return value ranges, array sizes, object validity, and nonzero divisors. The article relates these checks to preconditions and postconditions in design by contract: assertions document expectations and verify them while developing and debugging. It cautions that forced termination can leave chart indicators or other resources behind, and that assertions target programmer mistakes rather than external runtime problems. Production code should use appropriate error handling for issues that can occur independently of programming errors.

Key ideas

  • Assertions check conditions that should hold during normal program operation.
  • Conditional compilation can include checks during debugging and exclude them from release builds.
  • A failure can report the condition and source location, and can either alert or stop execution.
  • Assertions can document and check preconditions, postconditions, input ranges, and object state.
  • Assertions are for finding programming errors; production error handling should address other failures.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.