FutureBasic Logo

<<    Index    >> FutureBasic 5

on error fn / gosub   statement



Syntax:
on error {fn userFunction{(fileID, errorCode)}|gosub{lineNumber|"stmtLabel"}}

Description:
This statement designates and enables the routine that FutureBasic will call when certain kinds of errors occur. There may only be one on error vector. If you use a second call to on error fn, the new routine replaces the old version in subsequent calls. However you can deactivate or reactivate error trapping as often as you need. Using the on error end/on error return statements you can switch between the default behavior and your error handler.

Example:
/*
// Standard On Error handler without parameters
local fn myErr
stop "error happened"
end fn
*/

// optional On Error handler accepts two parameters
local fn myErr(id as long, e as long)
stop "id:"+str$(id)+" - e:"+str$(e)
end fn

dim as long test

on error fn myErr

read #2, test

Note:
If you use the on error fn userFunction syntax, then userFunction must refer to a function which was defined or prototyped at an earlier location in the source code. The error handling function accepts two optional parameters, fileID and errorCode, but doesn't return any result.
errorCode will be set to the error code issued for the failure ( i.e. -38 is file not open, -39 end of file error, -43 file not found etc.)

See Also:
on error end; on error return; error function