4D v16.3

ASSERT

Home

 
4D v16.3
ASSERT

ASSERT 


 

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

The ASSERT command evaluates the boolExpression assertion passed in parameter and, if it returns false, stops the code execution with an error message. The command works in interpreted and compiled mode.

If boolExpression is true, nothing happens. If it is false, the command triggers the error -10518 and displays by default 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.

Optionally, you can pass a messageText parameter to display a custom error message instead of the text of the assertion. 

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 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)

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"



See also 

Asserted
Get assert enabled
SET ASSERT ENABLED

 
PROPERTIES 

Product: 4D
Theme: Interruptions
Number: 1129

This command modifies the Error system variableThis command can be run in preemptive processes

 
HISTORY 

Created: 4D v12

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)