Default exception handling
Default exception handling must be adapted to your programming pattern.
Default program behavior when exception occurs
By default, when a language or SQL error occurs,
the program stops and shows a message to the end user. However, expression errors like division by
zero or invalid type conversions will not stop the program. In other words, the default is
WHENEVER ERROR STOP + WHENEVER ANY ERROR CONTINUE
.
In TUI mode, with default exception handlers, the error message is displayed in the terminal.
gui.programStoppedMessage
FGLPROFILE
entry:gui.programStoppedMessage = "An unexpected error occurred, program will stop."
Enforce expression error handling
By default, the WHENEVER ANY ERROR
action is to CONTINUE
the
program flow. This means that when a program makes for example a division by zero or when a string
cannot be converted to a number, the program continues. This can lead to unexpected program
behavior, but is the default to be backward compatible for legacy applications.
To make your code more robust, use WHENEVER ANY ERROR STOP
.
WHENEVER ERROR
exception class, with the following FGLPROFILE
entry:fglrun.mapAnyErrorToError = true
When this entry is set to true, expression errors
such as a division by zero will be trapped and execute the action defined by the last
WHENEVER ERROR
instruction.
When using the default exception handler (WHENEVER ERROR STOP
), the program with
stop at any expression error, and display the corresponding error message.
FGLPROFILE file:
fglrun.mapAnyErrorToError = true
MAIN
DEFINE x INT
WHENEVER ERROR CALL my_error_handler
LET x = 1 / 0 -- error handler will be called here
DISPLAY "It continues...."
END MAIN
FUNCTION my_error_handler()
DISPLAY "Handler: ", STATUS
END FUNCTION