14 hours ago, WitchLord said:
I won't be implementing a catch for specific exception types in the core library. Today you're asking for std::exception, tomorrow someone else will ask for another, and so.
I will however consider allowing applications to do this through a callback. perhaps using what Solokiller suggested, which looks quite nice, or else allow the application to provide a custom exception handling code that would be injected into the angelscript code at compile time with an #include statement.
I was initially going to argue that any self-respecting c++ library will use std::exception as a base type for all of their exceptions. But after cooling down for a while I can definitely see that allowing that might open the gates for requests of other exception types and a generic callback gives more flexibility.
After looking at solokiller's suggestion I decided to attempt to implement such a callback. And I got it working.
I attached a diff of my changes. I basically duplicated what asCContext::SetExceptionCallback does to make a new "Translate exception" callback, which I placed into a new method "HandleExceptionHappened", I had to add this as c++ exceptions were caught in multiple places. So now setting that callback to something like this:
void ScriptTranslateExceptionCallback(asIScriptContext* context, void* userdata)
{
try {
std::rethrow_exception(std::current_exception());
} catch(const std::exception& e) {
context->SetException(
(std::string("Caught application exception: ") + e.what()).c_str());
}
}
ctx->SetTranslateExceptionCallback(asFUNCTION(ScriptTranslateExceptionCallback), nullptr, asCALL_CDECL);
Results in nicer script exception messages: "Caught application exception: Testing custom messages"
as_exception_translate.diff