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.
ctx->
Prepare(engine->GetModule(
"test")->GetFunctionByName(
"func"));
{
if( err == "Caught an exception from the application" )
{
...
}
}
@ 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.
{
try
{
throw;
}
catch( std::exception &e )
{
}
catch(...)
{
}
}
@ 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.