13.06.2016, 14:18
i_errors
Download
IntroductionDownload
Statements like "sleep" (not working) and "assert" put the AMX machine (the one that executes compiled Pawn scripts) in a special state, breaking execution in the middle of the function (if assertation fails) and showing an error message. I've always wondered how these functions are implemented, and it turns out they are all done using the "halt" instruction (also sleep puts the argument into the primary register). It turns out any possible AMX error can be raised using this instruction, so I've made a list of all possible error codes, put them into an include and equipped it with a supplementary raise function, which triggers an error "#emit-free" for the user.
Error codes
All these codes can be accessed via a simple name:
Code:
enum { AMX_ERR_NONE, AMX_ERR_EXIT, /* forced exit */ AMX_ERR_ASSERT, /* assertion failed */ AMX_ERR_STACKERR, /* stack/heap collision */ AMX_ERR_BOUNDS, /* index out of bounds */ AMX_ERR_MEMACCESS, /* invalid memory access */ AMX_ERR_INVINSTR, /* invalid instruction */ AMX_ERR_STACKLOW, /* stack underflow */ AMX_ERR_HEAPLOW, /* heap underflow */ AMX_ERR_CALLBACK, /* no callback, or invalid callback */ AMX_ERR_NATIVE, /* native function failed */ AMX_ERR_DIVIDE, /* divide by zero */ AMX_ERR_SLEEP, /* go into sleepmode - code can be restarted */ AMX_ERR_MEMORY = 16, /* out of memory */ AMX_ERR_FORMAT, /* invalid file format */ AMX_ERR_VERSION, /* file is for a newer version of the AMX */ AMX_ERR_NOTFOUND, /* function not found */ AMX_ERR_INDEX, /* invalid index parameter (bad entry point) */ AMX_ERR_DEBUG, /* debugger cannot run */ AMX_ERR_INIT, /* AMX not initialized (or doubly initialized) */ AMX_ERR_USERDATA, /* unable to set user data field (table full) */ AMX_ERR_INIT_JIT, /* cannot initialize the JIT */ AMX_ERR_PARAMS, /* parameter error */ AMX_ERR_DOMAIN, /* domain error, expression result does not fit in range */ AMX_ERR_GENERAL, /* general error (unknown or unspecific error) */ }
Code:
stock raise(errcode)
Because of the way this function is implemented (a simple switch statement), only defined error codes can be used. Other codes will default to -1 (unknown). I guess raising any possible error code is doable, but it would require either a huge switch or modifying the running code, neither of which's costs are outweighed by the benefits.
Code:
stock errormsg(errcode)