0
我想在我的C++應用程序中使用v8。我陷入了helloworld本身!使用TryCatch時v8 hello world中的分段錯誤
在https://developers.google.com/v8/get_started的helloworld工作得很好。現在我正試圖在代碼中捕獲異常/錯誤。所以我用TryCatch trycatch;。
int main(int argc, char *argv[]) {
HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
TryCatch trycatch; /* TO CATCH EXCETIONS/ERRORS */
Handle<String> source = String::New("xyz();");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
if (result.IsEmpty()) {
fprintf(stderr, "Exception: %s\n",
*String::AsciiValue(trycatch.Exception()));
return -1;
}
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
context.Dispose();
return 0;
}
異常被捕獲正常,但程序沒有正常終止。它會產生分段錯誤。我究竟做錯了什麼?
你確定'String :: AsciiValue(trycatch.Exception())'是一個合適的字符串嗎?嘗試'char * s = * String :: AsciiValue(trycatch.Exception());',並檢查's'在調試器中是否有效。 – 2012-07-31 06:04:58
你不是在尋找'trycatch.Message()'嗎?還要注意,它們可能沒有與異常相關的消息,然後'返回一個空的句柄',因此您可能也需要檢查它。 – 2012-07-31 07:37:45