Emscripten 'val.h' API 允许调用 JS 对象的方法,但是 C++ try-catch 不会捕获 JS 异常。考虑这个例子:
#include <emscripten.h>
#include <emscripten/val.h>
void test(){
string t = "some invalid json";
val v = val::object();
// This C++ try-catch doesn't catch JS exception
try {
v = val::global("JSON").call<val>("parse", t);
cout <<"ok" <<endl;
}
catch(...){
cout <<"failed" <<endl;
}
cout <<"ret" <<endl;
}
JS 异常使“test”函数停止,并且没有 ok
、没有 failed
、没有 ret
打印出来。如何捕获 JSON.parse 抛出的 JS 异常?
这里有 1 个问题,但仍处于开放状态:https://github.com/emscripten-core/emscripten/issues/11496
最佳答案
基于documentation :
By default, exception catching is disabled in Emscripten.
你必须enable it使用 -fexceptions
参数。
关于javascript - Emscripten:如何捕获JS异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70894860/