AngelScript
 
Loading...
Searching...
No Matches
C++ exceptions and longjmp

Exceptions

Application functions and class methods registered with the script engine are allowed to throw C++ exceptions. The virtual machine will automatically catch any C++ exception, abort the script execution, and return control to the application.

asIScriptContext *ctx = engine->CreateContext();
ctx->Prepare(engine->GetModule("test")->GetFunctionByName("func"));
int r = ctx->Execute();
{
string err = ctx->GetExceptionString();
if( err == "Caught an exception from the application" )
{
// An application function threw an exception while being invoked from the script
...
}
}
@ asEXECUTION_EXCEPTION
The execution was terminated by an unhandled script exception.
Definition: angelscript.h:485
The interface to the virtual machine.
Definition: angelscript.h:2764
virtual const char * GetExceptionString()=0
Returns the exception string text.
virtual int Prepare(asIScriptFunction *func)=0
Prepares the context for execution of the function.
virtual int Execute()=0
Executes the prepared function.

By default the VM has no way of distinguishing between different types of exceptions and will just give a standard exception string for all of them. If desired a callback can be registered with the engine to provide a translation of the exception type to a more informative exception string.

void TranslateException(asIScriptContext *ctx, void* /*userParam*/)
{
try
{
// Retrow the original exception so we can catch it again
throw;
}
catch( std::exception &e )
{
// Tell the VM the type of exception that occurred
ctx->SetException(e.what());
}
catch(...)
{
// The callback must not allow any exception to be thrown, but it is not necessary
// to explicitly set an exception string if the default exception string is sufficient
}
}
// Register the callback with the engine
engine->SetTranslateAppExceptionCallback(asFUNCTION(TranslateException), 0, asCALL_CDECL);
@ asCALL_CDECL
A cdecl function.
Definition: angelscript.h:305
#define asFUNCTION(f)
Returns an asSFuncPtr representing the function specified by the name.
Definition: angelscript.h:708
virtual int SetException(const char *info, bool allowCatch=true)=0
Sets an exception, which aborts the execution.
See also
GetExceptionInfo helper function
Note
The ability to catch exceptions can be turned off by compiling the library with the AS_NO_EXCEPTIONS defined. If this is done, the application should not register any functions that may throw exceptions, as the end result will be undefined should an exception occur.

longjmp

Some applications uses longjmp to do error handling. When performing a longjmp to a previously saved state, there is no chance for the code to do a cleanup of all that happened after the saved state. Because of that the application must not register functions that can perform a longjmp out from the function, as that can leave the virtual machine in an undefined state.