GLBasic User Manual

Main sections

TRY

TRY
...
IF ... THEN THROW "Error"
...
CATCH err$
DEBUG err$ + "\n"
ASSERT 0
FINALLY
...



The TRY/THROW/CATCH/FINALLY commands are used to test if a section of code works, and to perform an action if it doesn't.

To mark the start of a section of code that you want to test you use the TRY command. End the TRY block with the FINALLY command.

Example:
TRY
    FOR i=0 TO 5
        STDOUT "call "+i+"\n"
        int_div_by_2(i)
    NEXT
CATCH err$
    STDERR "Exception: " + err$ + "\n"
FINALLY

STDOUT "ok, done\n"
KEYWAIT
END

FUNCTION int_div_by_2: number%
IF MOD(number%, 2)<>0 THEN THROW "Odd: "+number%
STDOUT "dividing "+number%+"\n"
RETURN number / 2
ENDFUNCTION

See also...