Can't catch exception


#1

I tried to put a try/catch around the call to the script that was breaking due to their not being an operator = available so that I could display the error to the user.
However, the code just seems to go bad inside chaiscript_common.hpp in the ‘throw’ part of the Boxed_Value eval(…) function to the point where xcode indicates a BAD_INSTRUCTION.

How do I catch these exception?

Thanks


#2

hm i’m not sure what you mean here. can you post a minimal example that compiles and shows this error to test this?
(chaiscript thows nicely for me)


#3

Probably not easily — the code is deeply distributed all over our files so consolidating into a simple example would take a lot of time. However, generally the only reason try/catch doesn’t work is if the code somehow ended up in a different thread. I’m wondering if that’s a possibility


#4

Silly question : Are you providing a chaiscript::exception_specification to eval method ?

like this :

try
{
	pChaiEngine_->eval(str, chaiscript::exception_specification<const std::exception&>());
}
catch (const std::exception& e)
{
	std::wcout << std::string(e.what());
}

#5

Not a silly question at all but no, I was not. All I am doing so far is

try
{
   eval....
}
catch(...)
{
   Console::Log("Error in script");
}

The goal being to just catch anything — once that worked, I would have refined so as to capture specfic issues and report what went wrong. Shouldn’t that work?


#6

You can try this :

try
{
	pChaiEngine_->eval(str, chaiscript::exception_specification<const std::exception&, const chaiscript::Boxed_Value&>());
}
catch (const std::exception& e)
{
	Console::Log("Error in script");
}
catch (const chaiscript::Boxed_Value& e)
{
	Console::Log("Error in script");
}

you should catch all chaiscript errors


#7

I’ll give it a shot but shouldn’t my generic catch(…) have worked regardless of the exception? Or is it necessary to include that exception specification to make exception handling work at all?


#8

Yeah, you should easily be able to catch any exception that ChaiScript is throwing from your C++, particularly with a catch(...). The only way I can see this not working is if you somehow have a bad / mismatched build.


#9

ok - figured out why I wasn’t catching some exceptions — stupid ID Ten T issue (sigh)

I am however wondering about the purpose of the “chaiscript::exception_specification<const std::exception&>()” argument in eval. I’m not using that and I’m still getting exceptions back properly


#10

The exception_specification will automatically unbox exceptions that are thrown from within script, as opposed to by the scripting engine.

example:

// c++ code
try {
  chai.eval("throw(5)");
} catch (const Boxed_Value &) {
  // this is catch that's executed
} catch (int) {
  // and not this one as expected, perhaps
}

However if we do an exception specification:

// c++ code
try {
  chai.eval("throw(5)", exception_specification<int>());
} catch (const Boxed_Value &) {
  // this catch is not executed
} catch (int) {
  // this catch is, as we might have wanted.
}

-Jason