我正在为 ARM 处理器编写 QT 应用程序。我使用 gcc-linaro-arm-linux-gnueabihf 编译器。
我 try catch std::exception
try
{
----CODE HERE----
}
catch(QException e)
{
qCritical() << e.what();
}
catch(std::exception e)
{
qCritical() << e.what();
}
作为输出我有:
----- Dev started -----
std::exception
----- Dev finished -----
没有关于异常的详细信息。
我能看到发生了什么样的 std::excpetpion 吗?
另一个问题,它是 qt 应用程序。 Makefile 由 qmake 生成。我无法直接将选项传递给 gcc 编译器。
最佳答案
问题是您正在切片异常对象——您是按值捕获它,所以任何子类信息都丢失了。通过 const &
捕获它以保持其类型和数据有效:
catch(const std::exception &e)
{
qCritical() << e.what();
}
另外,如果你想对更具体的类型(派生自 std::exception
的类)进行特殊处理,你可以添加它:
catch (const std::invalid_argument &e)
{
qCritical() << "Invalid argument: " << e.what();
}
catch (const std::domain_error &e)
{
qCritical() << "Domain error: " << e.what();
}
catch (const std::excetion &e)
{
qCritical() << "Other exception: " << e.what();
}
请注意,catch
子句的顺序很重要:它们按顺序处理,并使用第一个匹配项。所以派生类必须在基类之前列出。
关于c++ - 来自 std::exception 的更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35034196/