KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generating an Error
PRODUCT: 4D | VERSION: 20 R2 | PLATFORM: Mac & Win
Published On: November 6, 2023

Defensive coding and error handling are important features to implement when developing a robust system. To better test error handlers and issues, 4D v20R2 Introduced a new feature to generate errors. An error dialog can be triggered by using the new throw command. The command can be used in two ways, based on its parameters.

The first is the more simplistic use, by passing in up to two parameters. The first parameter is an integer error code and the second optional parameter is a text description of the error.

An example of this use case is:

throw(40; "Example 4D Error with Error Code 40")

This will generate a simple error dialog with an error code of 40 and a desciption of the error as "Example 4D Error with Error Code 40"

The second method is to build an object with up to four properties to better describe the error. The four properties are:
  • componentSignature - A text four letter code of the component, which defaults to "host" for the host or "C" followed by a three digit number starting with "001" for each component.

  • errCode - A numeric error code, which defaults to -1.

  • message - A text desciption of the error, the message can use placeholders with the properties of the object as parameters in curly braces {}

  • deferred - A boolean that will deffer the error if True allowing errors to stack.


An example of this use case is:
$objVar.componentSignature := "TEST"
$objVar.errCode := "40"
$objVar.message := "Hello {examplePropertyforMessage}, error on {componentSignature}"
$objVar.deffered := True
$objVar.examplePropertyforMessage:= "World"
throw($objVar)

This will result in an error with the properties defined, for the message property, the parameters will evaluate if they are defined in the object, so the message will be: "Hello World, error on TEST".

These features can be used to add new ways of defensive coding, test error handlers, and more.