## Error trapping using :Try..:EndTry

Syntax:

    :Try
    ...
    :CatchIf <boolean expression>
    ...
    :CatchAll
    ...
    :EndTry

The block of code following the `:Try` keyword is executed, until either an error occurs, or a `:CatchIf`, `:CatchAll`, `:End` or `:EndTry` is encountered.

If no error has occurred within the `:Try` block, execution transfers to the statement after the `:End` or `:EndTry`.

If an error occurs in the `:Try` block (either in the statements in this function, or in any functions called from it), control transfers to the first `:CatchIf` statement (if any), and the boolean expression is evaluated. If it is true, the block of code following the `:CatchIf` is executed, and execution then resumes after the `:EndTry` or `:End`. If the expression is false, the same procedure is followed for any further `:CatchIf` blocks in the sequence. If none of the tests is true, the `:CatchAll` block (if any) is executed. It is permissible to have as many `:CatchIf` sections as you like.

Once an error has been trapped and control passed to a `:CatchIf` or `:CatchAll` statement, the error trap is disabled. Thus, if a second error occurs, it will not be trapped, and the function will stop in the normal way (unless the whole `:Try... :EndTry` sequence is itself executed under another error trap). `:Try..:EndTry` blocks can be nested.

Typically, you use the `:CatchIf` statement to catch specific types of error, by looking at ŒLER or ŒET.

For example:

          ’R„A DIVIDE B
    [1]  © Protected divide
    [2]   :Try
    [3]     © Do division under error-trapped conditions
    [4]     R„A÷B
    [5]   :CatchIf 11=†ŒLER
    [6]     © DOMAIN ERROR occurred
    [7]     R„0
    [8]   :CatchAll
    [9]     © Some other error occurred
    [10]    'Unexpected error. The message was:'
    [11]    ' ',ŒEM
    [12]    …
    [13]  :EndTry
          ’

          4 DIVIDE 3
    1.333333333
          4 DIVIDE 0
    0
          DIVIDE 4
    Unexpected error. The message was:
     VALUE ERROR
     DIVIDE[4]   R„A÷B
                   ^

Topic: APLX Help : Help on APL language : Errors : Error trapping using :Try..:EndTry

[ Previous | Next | Contents | Index ]