Skip to content
Paul Squires edited this page Oct 14, 2026 · 2 revisions

Home › Miscellaneous Keywords › Assert

Assert

Debugging macro that halts program execution if an expression is evaluated to 0 (false).

Syntax

#define Assert(expression) If (expression) = 0 Then : fb_Assert( __FILE__, __LINE__, __FUNCTION__, #expression ) : End If

Parameters

  • expression
  • expression

Description

The Assert macro is intended for use in debugging and works only if the -g or -eassertoption is specified on thecompilercommand line. In this case it prints an error message and stops the program execution ifexpressionevaluates to0

.

Its normal use is to check the correct value of the variables or expressions during debugging.

If

-gand -eassert are not passed to the compiler, the macro does not generate any code, and has no effect.

Note: If an Assert fails while the program is in a graphics Screen (Graphics), the error message will not be visible as it will be printed to the graphics screen, which will be closed immediately after.

Remarks

Usage

Assert( expression )

Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __ASSERT.

Differences from QB

  • New to FreeBASIC

See also

Examples

Sub foo

 Dim a As Integer

 a=0

 Assert(a=1)

End Sub

foo

'' If -g or -eassert is used, this code stops with: test.bas(3): assertion failed at FOO: a=1

Category: Miscellaneous Keywords
Kind: keyword


Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.

Clone this wiki locally