📄 21-4.txt
字号:
/* 范例:21-4 */
#include <iostream.h>
class super{public: super(){cout << "super constructor";}};
void test1(int a) throw(super)
{ // 当 y = 0 时把错误信息类class super送出去
if(a == 0) throw super();
}
int main()
{
try
{
int x, y;
cout << "Please Input Two Number, x/y: \n";
cin >> x >> y; /* 当输入y的数值是0时,会发生异常并激活异常处理类 */
test1(y);
cout << "x/y = " << x/y << endl;
}
catch(super& e)
{
cerr << "\n这是自定义的错误信息,防止除数y等于0!!";
}
puts("\n按任意键继续...");
getchar();
}
Project p21-4.exe raised exception class super with message 'Exception Object Address: 0x683446'.Process stopped. Use Step or Run to continue.
(上列讯息仅在Debugger设定核选Stop on C++ Exceptions才会出现)
程序执行结果:
再编译一次会输出自定义的错误信息﹕
Please Input Two Number, x/y:
5
0
super constructor
这是自定义的错误信息,防止除数y等于0!!
按任意键继续...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -