Advertisement

AngelScript and C++ exceptions

Started by December 21, 2010 02:35 PM
4 comments, last by Solokiller 7 years, 9 months ago
Hi,
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?
Yes. It will be left in an undefined state, which will most likely crash your application or at the very least cause memory leaks.

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

Advertisement
Thanks!

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]

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

Advertisement

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

If it's not already you should also document the behavior of longjmp when in a function call. Exception handling on Windows can stop that, not sure about other platforms. It also leaves contexts in an undefined state.

This topic is closed to new replies.

Advertisement