【发布时间】:2016-05-19 07:18:48
【问题描述】:
所以,我正在做一个家庭作业,我们必须测试我们的班级。我们被禁止使用 stl。:) 问题是在 test_2 程序崩溃了。它一直有效,直到它回到主要的 try 语句。当它应该跳转到异常时,它就会崩溃。知道为什么会这样吗?提前致谢! :)
void test_2() //
{
int tomb[400];
for(unsigned int j=0;j<400;j++){
tomb[j]=j;
}
cout <<"array loaded \n";
Buffer<int> test2(40,"test2.txt","w");
Buffer<int> test21(40,"test21.txt","w"); // 40 meretu buffer
cout << "bufferek letrehozva \n";
for(unsigned int j=0;j<400;j++){
test2[j]=tomb[j];
}
cout << "buff loaded \n";
/* for(unsigned int j=0;j<400;j++){
cout << test2[j] << endl;
} */
test21=test2;
cout << "copy constr ran \n";
unsigned int i=0;
for(unsigned int j=0;j<400;j++){
if(i==39){for(i=0;i<40;i++){ //40 size buff
test21.printfile(test21[i]);}
i=0;}
else i++;
}
throw "gets here";
}
int main()
{
try
{
int number;
cin >> number; // nr of the test
switch (number)
{
case 1:
test_1(); // file_test
break;
case 2:
test_2(); // copyconst_test
break; // crashes here
case 3:
test_3();
break;
}
}
catch (exception& e)
{
cerr << e.what() << endl;
}
catch (int i)
{
cerr << i << endl;
}
catch (const char* s)
{
cerr << s << endl;
}
catch (...)
{
cerr << "*** Nagy baj van! ****" << endl;
}
return 0;
}
【问题讨论】:
-
是的:/只要达到最大值,它的大小就会翻倍。
-
这可能是很多事情,也许你的 Buffer 可能在析构函数中抛出另一个异常?如果您注释掉
throw "gets here";行,会发生什么? -
另外,通过 const 引用捕获 std::exception,即
catch (const exception& e)。
标签: c++ class testing crash try-catch