... end try
blocks are how errors are handled in TMBASIC.
Errors are raised using the throw
If an error is raised inside a try
block, then program execution will jump to the catch
block in order to handle the error.
You can display the error to the user or take some other corrective action like retrying.
in your catch
block to re-raise an error that you want to pass up to the calling code.
- <TRY-STATEMENTS> is the code to execute that may potentially fail with an error.
- <CATCH-STATEMENTS> is the code to execute if an error is raised inside the try block. It will not be executed if the try block executes successfully.
The following example is contrived to show both the "thrower" and "catcher" sides of a raised error.
The message string provided to the throw
statement can be retrieved from the ErrorMessage
function inside your catch
function can be used to retrieve the error code, which is optional for your own throw
statements but is always available for TMBASIC-generated errors.
throw 1234, "This is an error!"
print ErrorCode(); ": "; ErrorMessage()
In real code, the throw
statement will be inside a function or subroutine, and the catch
block will be in the calling code.