4D v13.4

ASSERT

Home

 
4D v13.4
ASSERT

ASSERT 


 

ASSERT ( boolExpression {; messageText} ) 
Parameter Type   Description
boolExpression  Boolean in Boolean expression
messageText  Text in Text of error message

The ASSERT command can be used to place an assertion in the code of a method.

An assertion is an instruction inserted in the code that is responsible for detecting any anomalies during its execution. The principle consists in verifying that an expression is true at a given moment and, should the opposite occur, to cause an exception. Assertions are above all used to detect cases that should usually not ever occur. They are mainly used to detect programming bugs. It is possible to globally enable or disable all the assertions of an application (for example according to the type of version) via the new SET ASSERT ENABLED command. 

For more information about assertions in programming, please refer to the article concerning them on Wikipedia: http://en.wikipedia.org/wiki/Assertion_(computing)

The ASSERT command evaluates the Boolean expression passed as a parameter. If the expression is true, nothing happens. If it is false, the command triggers the error -10518 and displays the text of the assertion preceded by the message "Assert failed:". You can intercept this error via a method installed using the ON ERR CALL command, in order, for example, to provide info for a log file. 

The command accepts an optional second parameter that can be used to provide a text that will be displayed in the error message instead of the text of the Boolean expression when it is false.

Before carrying out operations on a record, the developer wants to make sure that it is actually loaded in read/write mode:

 READ WRITE([Table 1])
 LOAD RECORD([Table 1])
 ASSERT(Not(Locked([Table 1])))
  // triggers error -10518 if record is locked

An assertion can allow parameters passed to a project method to be tested in order to detect aberrant values. In this example, a custom warning message is used.

  // Method that returns the number of a client according to its name passed in $1
 C_TEXT($1// Name of client
 ASSERT($1#"";"Search for a blank client name")
  // A blank name in this case is an aberrant value
  // If the assertion is false, the following will be displayed in the error dialog box:
  // "Assert failed: Search for a blank client name"

 
PROPERTIES 

Product: 4D
Theme: Interruptions
Number: 1129

This command modifies the Error system variable

 
INDEX

Alphabetical list of commands

 
HISTORY 

Created: 4D v12

 
SEE ALSO 

Asserted
Get assert enabled
SET ASSERT ENABLED