21-4.txt
来自「c和c++完美演绎,里面有编程的方法,对编程技术的提高有很大的帮助」· 文本 代码 · 共 38 行
TXT
38 行
/* 范例: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 + =
减小字号Ctrl + -
显示快捷键?