Catch Segmentation fault in c++(在 C++ 中捕获分段错误)
问题描述
try-catch 块是否会捕获分段错误错误?
Does a try-catch block catch segmentation fault errors?
我正在使用下面给出的函数读取文本文件,但有时文件为空并且程序崩溃.我希望程序继续运行并在此文件为空或正在使用时提供另一个文件.
I am reading a text file using the function given below but sometimes the file is empty and the program crashes. I would like the program to continue running and provide another file when this file is empty or in use.
Path2D read_gesture(const char* filename)
{
Path2D path;
//MultiStrokeGesture MultiStrokes;
vector<string> text_file;
int no_of_paths=0;
std::ifstream ifs(filename);
for (std::string line; std::getline(ifs, line); )
{
no_of_paths=no_of_paths+1;
double a, b;
stringstream ss(line);
if (!(ss >> a >> b)) {cout<<"wrong format"<<endl;}
std::cout << "You said, " << a << ", " << b << ".
";
path.push_back(Point2D(a,b));
}
cout<<"saving gesture"<<endl;
return path;
}
我尝试了类似的方法:
Path2D path;
try
{
path=read_gesture("test.txt");
}
catch(int e)
{
path=read_gesture("test2.txt");
}
但程序仍然崩溃.可能是什么问题?
but the program still crashes. What might the problem be?
- 稍微修正一下,
catch中调用的文件与try中调用的文件不一样,是错别字.
- A little correction, the file called in
catchwas not same as that oftry, that was a typo.
推荐答案
C++ try-catch 块只处理 C++ 异常.分段错误之类的错误是较低级别的,try-catch 会忽略这些事件,其行为与没有 try-catch 块相同.
C++ try-catch blocks only handle C++ exceptions. Errors like segmentation faults are lower-level, and try-catch ignores these events and behaves the same as if there was no try-catch block.
这篇关于在 C++ 中捕获分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中捕获分段错误
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
