如果出现未处理的异常,C++是否提供了一种"显示"可视化内容的方法?
我想做的是做一些事情,assert(unhandled exception.msg())如果它真的发生(如下面的例子):
void foo() {
throw std::exception("Message!");
}
int main() {
foo();
}
Run Code Online (Sandbox Code Playgroud)
我希望这种代码不会立即终止(因为异常未处理),而是显示自定义断言消息(Message!实际上).
那可能吗?
当我运行下面显示的简单程序时,我在Cygwin和Ubuntu OS上得到不同的终端输出.
#include <cstdio>
#include <stdexcept>
#include <cmath>
using namespace std;
double square_root(double x)
{
if (x < 0)
throw out_of_range("x<0");
return sqrt(x);
}
int main() {
const double input = -1;
double result = square_root(input);
printf("Square root of %f is %f\n", input, result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Cygwin上,与Ubuntu不同,我没有收到任何表明抛出异常的消息.可能是什么原因?我是否需要为Cygwin下载一些内容,以便它能够处理异常情况?
我使用Cygwin版本1.7.30和GCC 4.9.0.在Ubuntu上,我有GCC 4.8.1版本13.10.我怀疑在这种情况下编译器的差异很重要.