Documentation Center

Error Handling

It was mentioned that the GoTo statement can be used in a constructive fashion to handle errors. The errors in question are the so-called intentional errors.

The principle is as follows. You ask Sax Basic to perform a loop, knowing that sooner or later it will fail for some reason. When that happens you use a GoTo statement to exit the loop without the whole macro failing. The code for such an error handling could look like this:

Sub CopyFiles(SrcName As String, DstName As String)
  On Error GoTo FileNotFound

  FileCopy SrcName, DstName

FileNotFound:
  MsgBox "File not found: " & SrcName
End Sub

This example copies a file named SrcName to a new file named DstName. If the file SrcName is not present, this causes and error. This error leads to a GoTo statement, and we can handle the error in a suitable way.