AngelScript and C++ exceptions
what will currently happen in AngelScript if an exposed C++ object method or global function throws an exception while executing? Will the script context or engine be left in somehow undefined state?
Github: https://github.com/cadaver C64 development: http://covertbitops.c64.org/
If you plan on exposing functions/methods that can throw C++ exceptions you should wrap those in a function that catches the exception and transforms it into a script exception. E.g.
void foo_wrapper(){ try { foo(); } catch(...) { asIScriptContext *ctx = asGetActiveContext(); if( ctx ) ctx->SetException("Unknown exception"); }}
Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
I'll do either that, or wrap my exception generation so (where possible) that it checks for the active context, and sets a script exception instead of throwing when called from script.
EDIT: the exception wrapping works fine for most cases, except for constructors which would leak memory. For anyone interested, my method consists of macros and a function: (Note the need for a dummy return value for functions that return something)
#define SAFE_EXCEPTION(what) { checkAndThrowException(what); return; }#define SAFE_EXCEPTION_RET(what, ret) { checkAndThrowException(what); return ret; }void checkAndThrowException(const std::string& what){ asIScriptContext* context = asGetActiveContext(); if (context) { static std::string lastWhat; lastWhat = what; context->SetException(lastWhat.c_str()); } else throw Exception(what);}
Because my exception code is also shared by executables (tools and such) that don't actually instantiate the AngelScript engine, I needed to change asGetActiveContext() so that it nullchecks the threadManager instead of asserting on it.
[Edited by - AgentC on December 23, 2010 8:54:31 AM]
Github: https://github.com/cadaver C64 development: http://covertbitops.c64.org/
You're responding to a 7 year old post. :)
The exception handling has already been implemented since a long time back.
AngelScript will not be left in an undefined state when the application function/method throws an exception. Instead the C++ exception will be caught, and then the internal state will be set to asEXECUTION_EXCEPTION and the function asIScriptContext::Execute will return this error code.
I see that I had totally forgotten to document this behaviour in the manual. I'll make sure to include this for the next release.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
It's not that easy. Just re-throwing the exception could cause a lot of errors since the current code in the engine is not designed for exception handling.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game