Exception Handling

23.08.2021

FPScript exception handling allows you to react flexibly to errors in your formulas. Exceptions are all errors that may occur during the execution of FPScript code as well as exceptions that have been explicitly generated using the Throw statement.

The following example carries out a division calculation and generates an exception if the divisor equals zero.

Arguments X, Y
If Y == 0 Then
    Throw "Divide by zero"
End
X / Y

With the Try...Catch statement, you can catch these types of exceptions and react accordingly. In the case of an error in a calculation, for instance, you can use an error message to prevent the calculation from being aborted. Instead, you can catch the error and, for example, return an empty result:

Try
    Return Integral(x)
Catch Exception
    Return Empty
End

The variable specified behind the Catch statement is assigned the "thrown" value when generating the exception using Throw. This can be of any data structure. The functions and operators built into FPScript pass a string with a description of the error as a value of the exception.

Exceptions are a powerful concept for error processing by distinguishing between a regular result and an exception. You should, however, only use exceptions to handle errors and not as an alternative for passing results. The reverse is also true. You should not use the formula's resulting value to pass error messages, but generate an exception for this instead.

Share article or send as email:

You might be interested in these articles