Exception cpp Exception cpp include iostream using namespace

  • Slides: 38
Download presentation

範例程式 檔案 Exception. cpp // Exception. cpp #include <iostream> using namespace std; // --- 函數

範例程式 檔案 Exception. cpp // Exception. cpp #include <iostream> using namespace std; // --- 函數 Divide() 的原型 -----------float Divide(float, float); // ----- 主程式 --------------int main() { float M, N; try // 「try 區塊」的開頭 { cout << "請輸入兩個數字, n" << endl; cout << "請輸入第一個數字 : " << endl; 9

範例程式 檔案 Fnc. Lib. cpp // Fnc. Lib. cpp #include <cmath> using std: : fabs;

範例程式 檔案 Fnc. Lib. cpp // Fnc. Lib. cpp #include <cmath> using std: : fabs; const float Epsilon = 1. 0 E-16; // --- 定義函數 Divide() --------------float Divide(float a, float b) { if (fabs(b) <= Epsilon) throw "除數為零!"; // 「throw 敘述」 return a / b; } 11

17. 4 丟擲enum實例做為異常物件 例如: 宣告enum資料型態Error. States: enum Error. States {OK, Over. Flow, Under. Flow};

17. 4 丟擲enum實例做為異常物件 例如: 宣告enum資料型態Error. States: enum Error. States {OK, Over. Flow, Under. Flow}; 定義一個Error. States實例,稱為State並初始化 為OK: Error. States State = OK; 18

範例程式 檔案 Enum. Ex. cpp 22 // Enum. Ex. cpp #include <iostream> using namespace std;

範例程式 檔案 Enum. Ex. cpp 22 // Enum. Ex. cpp #include <iostream> using namespace std; float Show. Vector. Element(float [], int); // 函數的原型 // 定義 enum資料型態Error. States enum Error. States {OK, Over. Flow, Under. Flow}; // ---- 主程式 -----------int main() { const int Size = 5; float V[Size]; int M; for (int i=0; i< Size; i++) V[i] = float(0. 5+ i*i)/2. 5;

24 catch (Error. States Error) // 「catch 區塊」的開頭 { if (Error==Over. Flow) cerr <<

24 catch (Error. States Error) // 「catch 區塊」的開頭 { if (Error==Over. Flow) cerr << "發生異常: Over. Flow" << endl; if (Error==Under. Flow) cerr << "發生異常: Under. Flow" << endl; } // 「catch 區塊」的結尾 catch (. . . ) // catch-all 區塊的開頭 {cerr << "發生了預計範圍之外的異常!" << endl; } // catch-all 區塊的結尾 return 0; }

範例程式 檔案 Enum. Fnc. Lib. cpp // Enum. Fnc. Lib. cpp #include <cmath> using std:

範例程式 檔案 Enum. Fnc. Lib. cpp // Enum. Fnc. Lib. cpp #include <cmath> using std: : fabs; // 定義 enum資料型態Error. States enum Error. States {OK, Over. Flow, Under. Flow}; const float Epsilon = 1. 0 E-16; extern const int Size = 5; 25

26 // 函數 Show. Vector. Element() 的定義 float Show. Vector. Element(float A[], int N)

26 // 函數 Show. Vector. Element() 的定義 float Show. Vector. Element(float A[], int N) { Error. States State = OK; // 定義enum實例 State if (N >= Size) { State = Over. Flow; throw State; // 「throw 敘述」 } if (N < 0) { State = Under. Flow; throw State; // 「throw 敘述」 } return A[N]; }

範例程式 檔案 Common. h //Common. h #ifndef COMMON_H #define COMMON_H #include <iostream> #include <iomanip> #include

範例程式 檔案 Common. h //Common. h #ifndef COMMON_H #define COMMON_H #include <iostream> #include <iomanip> #include <math> using namespace std; 31

32 namespace Exc. Name. Space { class Over. Flow { }; class Under. Flow

32 namespace Exc. Name. Space { class Over. Flow { }; class Under. Flow { }; const int Size = 5; const float Epsilon = 1. 0 E-16; } // - 函數 Show. Vector. Element() 的原型 float Show. Vector. Element(float [], int); #endif

範例程式 檔案 Obj. Ex. cpp 33 // Obj. Ex. cpp #include "Common. h" using namespace

範例程式 檔案 Obj. Ex. cpp 33 // Obj. Ex. cpp #include "Common. h" using namespace Exc. Name. Space; // ----主程式-----------int main() { const int Size = 5; float V[Size]; int M; for (int i=0; i< Size; i++) V[i] = float(0. 5+ i*i)/2. 5;

catch (Under. Flow) { cerr << "發生異常: Under. Flow" } catch (. . .

catch (Under. Flow) { cerr << "發生異常: Under. Flow" } catch (. . . ) { cerr << "發生了預計範圍之外的異常!" } return 0; } 35 << endl;

範例程式 檔案 Obj. Fnc. Lib. cpp 36 // Obj. Fnc. Lib. cpp #include "Common. h"

範例程式 檔案 Obj. Fnc. Lib. cpp 36 // Obj. Fnc. Lib. cpp #include "Common. h" using namespace Exc. Name. Space; // 函數 Show. Vector. Element() 的定義 float Show. Vector. Element(float A[], int N) { if (N>=Size) { throw Over. Flow(); } // 「throw 敘述」 if (N < 0) { throw Under. Flow(); } // 「throw 敘述」 return A[N]; }