小白求助 关于抛异常的问题
#include "iostream"using std::endl;
using std::cin;
using std::cout;
double divided(int a, int b)
{
if (b == 0)
{
throw a;
}
return a / b;
}
void myDivided(int a, int b)
{
try
{
divided(a, b);
}
catch (...)
{
cout << "我接收了divided的异常,但是我没处理\n";
throw;//问题1: 这里throw之后运行成功
}
}
void main()
{
try
{
myDivided(10, 2);
myDivided(40, 0);
}
catch (...)
{
cout << "其他异常\n";
}
system("pause");
}
#include "iostream"
using std::endl;
using std::cout;
using std::cin;
class Test
{
private:
int a;
int b;
public:
Test(int a, int b)
{
this->a = a;
this->b = b;
}
~Test()
{
cout << "析构函数执行了\n";
}
};
void objPlay()
{
Test t1(11, 22);
Test t2(33, 44);
cout << "准备抛异常\n";
throw;//问题:对比上面的运行成功 这里直接throw运行失败,是怎么一回事啊?
}
void main()
{
try
{
objPlay();
}
catch (...)
{
cout << "未知类型异常\n";
}
system("pause");
}