Error Handling

Previous  Next

 

There are a number of functions available to deal with error handling and reporting.  To handle and report errors you need to implement an error handler and tell MyBASIC2 interpreter about this error handler.

 

Implementing Error Handler

 

You need to implement an error handler with the following declaration

 

void _on_error( mb_interpreter_t* s, mb_error_e errno, char* msg, unsigned short row, unsigned short col, int abort_code )

 

Next you need to tell MyBASIC2 about your error handler:

 

mb_set_error_handler( bas, _on_error );

 

Generating an Error

 

To generate an error condition, use the function

 

mb_set_error( mb_interpreter_t* s, void** l, mb_error_e err );

 

Reporting Errors

 

To retrieve the error number use

 

mb_error_e mb_get_last_error( mb_interpreter_t* s );

 

To retrieve the error message

 

const char* mb_get_error_desc( mb_error_e err );

 

Adding Custom Error Messages

 

You can add your own error messages by using

 

int mb_add_error( mb_interpreter_t* s, const char* str );

 

This function will return the error number for your message.  You can use this with mb_set_error to generate your custom error and then retrieve the message using mb_get_error_desc

 


Example

 

/////////////////////////////////////////////////////////////////////////////

//

static void _on_error( mb_interpreter_t* s, mb_error_e e, char* m, unsigned short row, unsigned short col, int abort_code )

{

  char* type[2] = { "ERROR", "WARNING" };

 

  if ( SE_NO_ERR != e )

  {

     printf( "%s: %s\nModule: %s,  Line: %d,  Col: %d\n", type[abort_code == MB_FUNC_WARNING], m, s->pModule, row, col );

  }

}

.

.

.

mb_set_error_handler( bas, _on_error );

.

.

.

int _title_case_func( mb_interpreter_t* s, void** l )

{

  // Parse Empty Parameters

  BEGIN_PARSE( s, l )

     POP_STRING( pStrPar )

  END_PARSE

 

  if ( strlen( pStrPar ) == 0 )

  {

     // Must not be zero length string.  Signal error and return with function error

     mb_set_error( s, l, SE_RN_STRING_EXPECTED );

     return MB_FUNC_ERR;

  }

.

.

.