Returning TI-Basic Errors

Previous Returning Values

Basically, you can return errors using ER_throw or ER_throwVar from error.h. Many functions from this library throw errors, which you can simply pass on to the operating system, possibly using TRY...FINALLY...ENDFINAL blocks.

However, for the system to operate normally after this, you need to write

#define ENABLE_ERROR_RETURN
at the beginning of your program. Otherwise, the screen will not be updated properly and other cleanup code (for example for USE_INTERNAL_FLINE_EMULATOR) is not executed. Most importantly, however, the program handle remains locked in AMS 1.xx because of a bug in the operating system. If you try to start the program again, you will get an "Invalid Program Reference" error.

Note: The workaround for this bug works only in kernel-less programs. For kernel-based programs, working around the bug is the kernel's responsibility. If you want to use this directive in kernel mode, you need to check which kernels perform such a workaround, and tell your users about this fact.

The following example (called "Memory Error") demonstrates how this directive may be used together with TRY...FINALLY...ENDFINAL blocks:
// Allocate memory as long as possible, then throw an error
// All allocated memory will be freed again!

#define USE_TI89              // Compile for TI-89
#define USE_TI92PLUS          // Compile for TI-92 Plus
#define USE_V200              // Compile for V200

#define MIN_AMS 100           // Compile for AMS 1.00 or higher
#define ENABLE_ERROR_RETURN   // Enable Returning Errors to TIOS

#include <tigcclib.h>         // Include All Header Files

#define BLOCK_SIZE 1024

void AllocRecursively(void)
{
  void *ptr = malloc_throw (BLOCK_SIZE);
  TRY
    // Could do something with ptr here...
    AllocRecursively ();
    // Could still do something with ptr...
  FINALLY
    free (ptr);
  ENDFINAL
}

// Main Function
void _main(void)
{
  AllocRecursively ();
}