c++的例外處理

看板Programming作者時間17年前 (2008/03/01 15:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
請教一個問題,如果我想在c++內做例外的判斷 為何我分子輸入5 分母輸入a 跑出5/2=2.5 而不是例外呢? 謝謝! 附上程式碼如下: #include <iostream> #include <stdlib.h> using namespace std; class fraction { private: int numerator; int denominator; public: void set_value() { cout << "輸入分子:"; cin >> numerator; cout << "輸入分母:"; cin >> denominator; } void print_value() { try { if (denominator==0) { throw 0; } else if (denominator<0) { throw " ~~分母<0 這樣是不行的 "; } else { cout << numerator << "/" << denominator << "="; cout << double (numerator)/double(denominator) << endl; } } catch(int err) { cout <<"有問題"<<endl; } catch(const char* str) { cout <<str<<endl; } catch(...) { cout <<"其他錯誤"<<endl; } } }; int main() { fraction X; X.set_value(); X.print_value(); system("pause"); return 0; } -- ┌─────KKCITY─────┐  找歌最方便 KKBOX 歌詞搜尋!! bbs.kkcity.com.tw \^_^ / http://www.kkbox.com.tw └──From:140.109.123.38 ──┘   唱片公司授權,音樂盡情下載 --
文章代碼(AID): #17oFym00 (Programming)
文章代碼(AID): #17oFym00 (Programming)