Gambas/Error

From Wikibooks, open books for an open world
Jump to: navigation, search

Error Handling

To avoid an error from crashing your program, you can execute the program line under provision and afterwards check if an error occured.

The line that might give an error is

doSomething(iParameter)

The first step is to use TRY

TRY doSomething(iParameter)

Then you have to catch the error:

IF ERROR
  doLogError(Error.Text)
ENDIF

The error handling can by anything, from DEBUG statements to show the error in the debug window while working on the program, over just logging the error somewhere, to more extensive error handling. The complete code becomes:

TRY doSomething(iParameter)
IF ERROR
  doLogError(Error.Text)
ENDIF

Typical use for TRY / IF ERROR is when doSomething is reading records from a database; the database connection might be broken and that might crash your program here.