查看传递给抛出异常的构造函数的字符串
View string passed to constructor of thrown exception
我正在尝试调试一个用 Emscripten 编译的 C++ 程序,它会抛出异常,特别是 runtime_error 将字符串作为 what_arg
传递。但是,当他们抛出时,我只是在 Javascript 控制台中得到一个数字(指针值?)输出。传递给构造函数的字符串会更有帮助。
例如程序
#include <stdexcept>
int main()
{
throw std::runtime_error("I want to see this in the console");
return 0;
}
使用 Emscripten 1.35.0 64 位(在 Mac OS X 上)通过命令编译
em++ exception.cc -o exception.html
在浏览器中查看时,输出到控制台
Uncaught 5247024
我如何在运行时看到 what_arg
参数是什么?
理想情况下,C++ 代码中没有 try-catch 块,因此我可以使用 DISABLE_EXCEPTION_CATCHING flag. There are ways of converting the memory address of a C-style string to a Javascript string, using Pointer_stringify。也许作为异常传递的数字有类似的东西?
您需要 catch
它并手动打印 what()
字符串。
编辑:这个 必须 在 C++ 中用 try
/catch
块完成,类似于:
int main(int argc, char** argv)
{
try
{
throw std::runtime_error("I want to see this in the console");
}
catch (const std::runtime_error& error)
{
std::cout << error.what() << std::endl;
}
return 0;
}
有一种方法使用window.onerror,似乎在抛出未处理的异常时调用。使用这个,我可以
- 获取 onerror 处理程序的第 5 个参数
- 如果不是数字则什么都不做
- 将数字传回 C++ 世界的函数,例如,ccall
- 函数然后对数字执行
reinterpret_cast
以获得指向 runtime_error
的指针
- 在
runtime_error
上调用 what
并将生成的字符串传递给 cerr
执行此操作的示例 C++ 程序是
#include <stdexcept>
#include <iostream>
#include <emscripten.h>
extern "C" void EMSCRIPTEN_KEEPALIVE what_to_stderr(intptr_t pointer)
{
auto error = reinterpret_cast<std::runtime_error *>(pointer);
std::cerr << error->what() << "\n";
}
int main()
{
throw std::runtime_error("I want to see this in the console");
return 0;
}
可以使用命令编译
em++ -std=c++11 exception.cc -o exception.js
和运行在一个简单的HTML页面中
<!doctype html>
<html>
<head>
<title>Exception test</title>
<script>
var Module = {};
window.onerror = function(message, url, line, column, e) {
if (typeof e != 'number') return;
var pointer = e;
Module.ccall('what_to_stderr', 'number', ['number'], [pointer]);
}
</script>
<script src="exception.js"></script>
</head>
<body>
</body>
</html>
它似乎适用于 Chrome 46 和 Firefox 41。
我正在尝试调试一个用 Emscripten 编译的 C++ 程序,它会抛出异常,特别是 runtime_error 将字符串作为 what_arg
传递。但是,当他们抛出时,我只是在 Javascript 控制台中得到一个数字(指针值?)输出。传递给构造函数的字符串会更有帮助。
例如程序
#include <stdexcept>
int main()
{
throw std::runtime_error("I want to see this in the console");
return 0;
}
使用 Emscripten 1.35.0 64 位(在 Mac OS X 上)通过命令编译
em++ exception.cc -o exception.html
在浏览器中查看时,输出到控制台
Uncaught 5247024
我如何在运行时看到 what_arg
参数是什么?
理想情况下,C++ 代码中没有 try-catch 块,因此我可以使用 DISABLE_EXCEPTION_CATCHING flag. There are ways of converting the memory address of a C-style string to a Javascript string, using Pointer_stringify。也许作为异常传递的数字有类似的东西?
您需要 catch
它并手动打印 what()
字符串。
编辑:这个 必须 在 C++ 中用 try
/catch
块完成,类似于:
int main(int argc, char** argv)
{
try
{
throw std::runtime_error("I want to see this in the console");
}
catch (const std::runtime_error& error)
{
std::cout << error.what() << std::endl;
}
return 0;
}
有一种方法使用window.onerror,似乎在抛出未处理的异常时调用。使用这个,我可以
- 获取 onerror 处理程序的第 5 个参数
- 如果不是数字则什么都不做
- 将数字传回 C++ 世界的函数,例如,ccall
- 函数然后对数字执行
reinterpret_cast
以获得指向runtime_error
的指针
- 在
runtime_error
上调用what
并将生成的字符串传递给cerr
执行此操作的示例 C++ 程序是
#include <stdexcept>
#include <iostream>
#include <emscripten.h>
extern "C" void EMSCRIPTEN_KEEPALIVE what_to_stderr(intptr_t pointer)
{
auto error = reinterpret_cast<std::runtime_error *>(pointer);
std::cerr << error->what() << "\n";
}
int main()
{
throw std::runtime_error("I want to see this in the console");
return 0;
}
可以使用命令编译
em++ -std=c++11 exception.cc -o exception.js
和运行在一个简单的HTML页面中
<!doctype html>
<html>
<head>
<title>Exception test</title>
<script>
var Module = {};
window.onerror = function(message, url, line, column, e) {
if (typeof e != 'number') return;
var pointer = e;
Module.ccall('what_to_stderr', 'number', ['number'], [pointer]);
}
</script>
<script src="exception.js"></script>
</head>
<body>
</body>
</html>
它似乎适用于 Chrome 46 和 Firefox 41。