?? 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 + -