catch, catch if

Prev Next

Function Names

catch, catch if

Synopsis

catch/catch if (expression, condition expression) { statements } [ else { statements } ]

Description

In case throw() has been called, all furhter code statements will be skipped until one of these functions are encountered.
catch will always give recover "frisbee" value issued with the throw() function, whereas
catch if will only catch the value if the "frisbee" value compared with the comparison expression returns true. Otherwise, the frisbee continues flying, i.e. the code further below will be skipped, including the 'else' block if added.

Call as: procedure

Restrictions

Indirect parameter passing is disabled

OS differences

None

Parameter count

1 (catch), 2 (catch if)

Parameters

No.TypeDescription
1
code
variable
:string
frisbee value caught

The frisbee value issued with the throw() function call will be recovered here.

2
code
comparison expression
:string
Values to compare with

Applicable to catch if: Catching the value and continuing normal execution will only happen if the thrown "frisbee" value matches with the comparison expression. This parameter is a piece of code typically found on the right-hand side of a comparison with '=' or '<>'. Single values, ranges (e.g. 3..5), multiple values separated by commas are supported. For text comparison, wildcards are supported if the string is of type softquoted string.

Examples

       define procedure( foo, {{ value, numeral }} )
       {
               if (value[]== 1) return; // No throw with value 1
               echo("Throw: ", value[] );
               throw( value[] );
               echo("This text does not appear");
       }

       for (i[] = 1, i[] <= 3, i[]++)
       {
               echo(new line, "Round ", i[],":");
               foo( i[] );

               catch if( x[], <>2 )
               {
                       echo("'catch if': Successful catch: ", x[] );
               }
               else
               {
                       echo("'catch if': Nothing thrown" );
               }

               catch ( x[]  )
               {
                       echo("'catch'   : Successful catch: ", x[] );
               }
               else
               {
                       echo("'catch'   : Nothing thrown" );
               }
       }

Output

Round 1:
'catch if': Nothing thrown
'catch'   : Nothing thrown

Round 2:
Throw: 2
'catch'   : Successful catch: 2

Round 3:
Throw: 3
'catch if': Successful catch: 3
'catch'   : Nothing thrown
Try it yourself: Open LIB_Function_catch.b4p in B4P_Examples.zip. Decompress before use.