exception

Prev Next

Function Names

exception

Description

This function asserts an exception in a similar way as programming errors are handled This feature is very useful if you want to assert an error message associated plausible info (including listing a few lines of code above the place where the exception is happening). The exception shows up like any other exception you typically encounter, for example divisions by zero or unable to find table columns.

Call as: procedure

Restrictions

Indirect parameter passing is enabled.

Parameter count

2-3

Parameters

No.TypeDescription
1
input
string Action

Supported values: pause, interactive, throw, stop, end, exit, abort
See the corresponding function names desribed in the previous section which correspond to the values above to learn more about the consequential behavior.

2
input
string Message

Provide the error message. If you need to compose multiple values like in an echo() call, then use the compose() funtion to put all values into them.

Opt. 3
input
numeral Calling hierarchy

The calling hierarchy determines the hierarchical calling level of the code where to eventually display the lines of code along with the exception message. The level increases by 1 with every call to user functions, and starting B4P programs with start or include.

0 The code where this exception call is made will be shown.
1 One level up, e.g. the code where the function call to this code with the exception call is made will be shown.
2, 3, ... further levels up
< 0 Suppress showing code lines

Default value: 0

Examples

       define function ( square root, { { n, numeral } } )
       {
               // If the last parameter is 0, then the code up here will be shown and not at the function call.
               if (n[] < 0) exception( stop, "1st parameter: Negative values are not allowed for square roots", 1 );
               return( sqrt(n[]) );
       }

       // Main Program

       {
               echo("Try a negative square root.");
               a[] = square root( -1 );
       }

Output

Try a negative square root.

Row #: Code Text _______________________________________________________
       7:    }
       8:
       9:    // Main Program
   10:
   11:    {
   12:        echo("Try a negative square root.");
   13:        a[] = square root( -1 );
                                                                        ^
1st parameter: Negative values are not allowed for square roots

Code execution will stop.  Going to interactive mode
________________________________________________________________________

Type 'help' for help, 'docs' for B4P docs, 'web docs' for online docs.
>>
Try it yourself: Open LIB_Function_exception.b4p in B4P_Examples.zip. Decompress before use.

See also

interactive