【发布时间】:2018-06-08 07:38:23
【问题描述】:
我指的是代码here。然后我将其修改如下,以捕捉失败。但是代码在没有捕获异常的情况下失败。如何处理(捕获)这种故障?
#include <iostream>
#include <exception>
using namespace std;
int main()
{
int test = 0;
cout << "test set to 0" << endl;
try {
cout << (test ? "A String" : 0) << endl;
} catch(...) {
cout << "Exception" << endl;
}
cout << "Test done" << endl;
return 0;
}
【问题讨论】:
-
常见的类型是指针,解引用空指针会导致未定义的行为而不是异常。如果它崩溃了,那是因为另一个较低级别的“异常”,而不是 C++ 异常。
标签: c++ exception try-catch ternary-operator