lang • catch

CATCH

SUB Function ( ... ) ... CATCH ... END

This instruction indicates the beginning of the error management part of a function or a procedure.

The catch part is executed when an error is raised between the beginning of the function execution and its end. This error can be raised by the function itself, or by any other function called during its execution, provided that this deeper function has no catch part itself: the deeper the catch part is, the more priority it has.

If an error is raised during the execution of the catch part, it is normally propagated. The catch part does not protect itself!

If there is a finally part in the function, it must precede the catch part. See FINALLY for more details.

Examples

' Prints a file to the screen

Sub PrintFile(FileName As String)

  Dim hFile As File
  Dim sLig As String

  Open FileName For Read As #hFile

  While Not Eof(hFile)
    Line Input #hFile, sLig
    Print sLig
  Wend

Finally ' Always executed, even if a error is raised

  Close #hFile

Catch ' Executed only if there is an error

  Print "Cannot print file "; FileName

End

See also