TRY/END TRY block

Purpose

A structured method of trapping and responding to run-time errors.

Syntax

TRY

  {statements}

  [EXIT TRY]

  {statements}

CATCH

  {error handling statements}

  [EXIT TRY]

  {error handling statements}

[FINALLY

  {statements}

  [EXIT TRY]

  {statements}]

END TRY

Remarks

Statements in the TRY section are executed normally.  The first time a run-time error occurs, control is transferred to the CATCH section.  If no run-time errors are generated in the TRY section, the CATCH section is skipped entirely.

Then, regardless of error status, the FINALLY section is executed, if it is present.  Error trapping and control transfer are disabled in the CATCH and FINALLY sections, so you would normally use conventional " IF ERR =…" tests to check the success of error-prone operations in those sections.

However, TRY structures can be nested to any level, so it may be desirable to use another TRY block within these clauses.

Restrictions

CATCH is a mandatory section of this structure, although the FINALLY section is optional.

Because of the nesting requirements, the ERR value is local to the TRY structure.  Upon exit, the prior ERR value is restored, so be sure to save the value of ERR if it will be needed outside of the TRY structure.

To leave the TRY structure, execution must pass normally through END TRY, or by an EXIT TRY statement.  Leaving a TRY block any other way is strongly discouraged because error trapping will remain disabled, and the previous ERR value will not be restored.  Future versions of PowerBASIC may disallow such practices.

ON ERROR GOTO is invalid within a TRY structure, but may be used within the same Sub/Function.

See also

#DEBUG ERROR, ERL, ERR, ERRCLEAR, ERROR, Error OverviewERROR$, Error TrappingON ERROR, Run-time Errors

Example

TRY

  OPEN "file.dat" FOR INPUT LOCK READ WRITE AS #1

CATCH

  CALL NotifyUserOfError(ERR)

  EXIT TRY

FINALLY

  CALL UpdateDataBase()

  CLOSE #1

END TRY