Whenever the Execute() method returns asEXECUTION_EXCEPTION, you can get the stack trace and program location where the exception occurred. You can also go so far as enumerating the value of the local variables at the moment if you wish.
Here's a function I use to get the location of the exception:
void PrintException(asIScriptContext *ctx){ asIScriptEngine *engine = ctx->GetEngine(); int funcId = ctx->GetExceptionFunction(); const asIScriptFunction *function = engine->GetFunctionDescriptorById(funcId); printf("func: %s\n", function->GetDeclaration()); printf("modl: %s\n", function->GetModuleName()); printf("sect: %s\n", function->GetScriptSectionName()); printf("line: %d\n", ctx->GetExceptionLineNumber()); printf("desc: %s\n", ctx->GetExceptionString());}
A more complete example, which enumerates the stack and the variables:
void PrintVariables(asIScriptContext *ctx, int stackLevel){ asIScriptEngine *engine = ctx->GetEngine(); int typeId = ctx->GetThisTypeId(stackLevel); void *varPointer = ctx->GetThisPointer(stackLevel); if( typeId ) { print(" this = 0x%x\n", varPointer); } int numVars = ctx->GetVarCount(stackLevel); for( int n = 0; n < numVars; n++ ) { int typeId = ctx->GetVarTypeId(n, stackLevel); void *varPointer = ctx->GetAddressOfVar(n, stackLevel); if( typeId == engine->GetTypeIdByDecl("int") ) { print(" %s = %d\n", ctx->GetVarDeclaration(n, stackLevel), *(int*)varPointer); } else if( typeId == engine->GetTypeIdByDecl("string") ) { CScriptString *str = (CScriptString*)varPointer; if( str ) print(" %s = '%s'\n", ctx->GetVarDeclaration(n, stackLevel), str->buffer.c_str()); else print(" %s = <null>\n", ctx->GetVarDeclaration(n, stackLevel)); } }}void ExceptionCallback(asIScriptContext *ctx, void *param){ asIScriptEngine *engine = ctx->GetEngine(); int funcID = ctx->GetExceptionFunction(); const asIScriptFunction *function = engine->GetFunctionDescriptorById(funcID); print("--- exception ---\n"); print("desc: %s\n", ctx->GetExceptionString()); print("func: %s\n", function->GetDeclaration()); print("modl: %s\n", function->GetModuleName()); print("sect: %s\n", function->GetScriptSectionName()); int col, line = ctx->GetExceptionLineNumber(&col); print("line: %d,%d\n", line, col); // Print the variables in the current function PrintVariables(ctx, -1); // Show the call stack with the variables print("--- call stack ---\n"); for( int n = 0; n < ctx->GetCallstackSize(); n++ ) { funcID = ctx->GetCallstackFunction(n); const asIScriptFunction *func = engine->GetFunctionDescriptorById(funcID); line = ctx->GetCallstackLineNumber(n,&col); print("%s:%s:%d,%d\n", func->GetModuleName(), func->GetDeclaration(), line, col); PrintVariables(ctx, n); }}