What happens when a function raises an exception?

Started by
1 comment, last by _Sigma 16 years, 11 months ago
suppose I have a function like the one below that acts as a wrapper:

void WriteToLog(std::string message)
{
		TLog::GetInstance().LogMessage("Script",message,MSG_INFO);	
}

LogMessage raises a TFatalException on an error - ie: when the log file is not open. I will eventually change this to a TGenericException or the like which is nonfatal. But for the moment this is to be a fatal exception. So what happens if LogMessage raises an exception in its execution by AS? There is a catch wrapper outside of the main execution loop.

WinMain(...){
try
{
   ...
   SetupEngine();
   engine->Main();
   ...
}
catch(TFatalException &e)
{
    ShowError();
    Terminate();
}
}
And from engine->Main() is where the main while(1) lies, which in turn is calling out to the main AS function which could potentially call the Log function, which is where an exception could be raised. So - how do I handle this? or is there no need to worry? Cheers _S
Advertisement
If you just want to log the error and then terminate the application, then you should be fine doing it the way you do.

But if you want to catch the exception and continue execution without memory leaks, then it's probably better to catch the exception already in the wrapper and instead call asIScriptContext::SetException() to tell AngelScript to end the script execution and clean up. The asIScriptContext::Execute() function will then return asEXECUTION_EXCEPTION (= 3).

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

Sounds good to me. Thanks.

This topic is closed to new replies.

Advertisement