?? 21-4.txt
字號:
/* 范例:21-4 */
#include <iostream.h>
class super{public: super(){cout << "super constructor";}};
void test1(int a) throw(super)
{ // 當(dāng) 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; /* 當(dāng)輸入y的數(shù)值是0時,會發(fā)生異常并激活異常處理類 */
test1(y);
cout << "x/y = " << x/y << endl;
}
catch(super& e)
{
cerr << "\n這是自定義的錯誤信息,防止除數(shù)y等于0!!";
}
puts("\n按任意鍵繼續(xù)...");
getchar();
}
Project p21-4.exe raised exception class super with message 'Exception Object Address: 0x683446'.Process stopped. Use Step or Run to continue.
(上列訊息僅在Debugger設(shè)定核選Stop on C++ Exceptions才會出現(xiàn))
程序執(zhí)行結(jié)果:
再編譯一次會輸出自定義的錯誤信息﹕
Please Input Two Number, x/y:
5
0
super constructor
這是自定義的錯誤信息,防止除數(shù)y等于0!!
按任意鍵繼續(xù)...
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -