【发布时间】:2016-03-09 19:10:36
【问题描述】:
我正在尝试使用 ifstream 和所有 try and catch 方法打开文件。我想测试读取功能。当我试图阅读更多文件长度时,我遇到了异常。但是错误按摩:
Error:basic_ios::clear: Success
我的代码:
void OpenFile(char* raw_slice) throw(std::ios_base::failure, std::string) {
ifstream init;
init.exceptions(ifstream::failbit | ifstream::badbit | ifstream::eofbit);
try {
init.open("/home/path/file", ios_base::in | ios_base::binary );
}catch(const ifstream::failure &e) {
cerr << "Error:" << e.what() << ": " << strerror(errno) << endl;
throw;
}
try {
init.read((char*)raw_slice, 1000); //out of bound of source file
}catch (const ifstream::failure &e){
cerr << "Error:" << e.what() << ": " << strerror(errno) << endl;
throw;
}
try{
init.close();
}catch(const ifstream::failure &e){
cerr << "Error:"<< e.what() << ": " << strerror(errno) << endl;
throw;
}
}
int main()
{
char* buf = new char[72];
try{
OpenFile(buf);
}catch (const ifstream::failure &e){
cerr << "Error in main:" << e.what() << ": " << strerror(errno) << endl;
delete[] buf;
return 0;
}catch (const string &e) {
cerr << "Error in main:: " << e << endl;
delete[] buf;
return 0;
``}
delete[] buf;
return 1;
}
任何想法为什么?我已经查看了 read 函数的返回值,但找不到此行为的答案。
【问题讨论】:
标签: c++ exception try-catch throw