我正在编写一个 JavaScript 应用程序,它通过 Google 的 V8 访问一些 C++ 代码。
一切正常,但我不知道如何抛出可以在 C++ 方法的 JavaScript 代码中捕获的 JavaScript 异常。
例如,如果我在 C++ 中有一个函数,例如
...
using namespace std;
using namespace v8;
...
static Handle<Value> jsHello(const Arguments& args) {
String::Utf8Value input(args[0]);
if (input == "Hello") {
string result = "world";
return String::New(result.c_str());
} else {
// throw exception
}
}
...
global->Set(String::New("hello"), FunctionTemplate::New(jsHello));
Persistent<Context> context = Context::New(NULL, global);
...
暴露于 JavaScript,我想在 JavaScript 代码中使用它
try {
hello("throw me some exception!");
} catch (e) {
// catched it!
}
从 C++ 代码中抛出 V8 异常的正确方法是什么?