C++ 异常
调用abort处理异常
#include <iostream>using namespace std;// 调用abort处理异常double hmean(double a, double b){ if (a == -b) { cout << "untenable argument to hmean()\n"; abort(); } return 2.0 * a * b / (a + b);}void errorOne(){ double x, y, z; cout << "Enter two numbers: "; while (cin >> x >> y) { z = hmean(x, y); cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; cout << "Enter next set of numbers <q to quit>: "; } cout << "Bye!\n";}int main() { errorOne(); return 0;}
返回错误码
#include <iostream>#include <cfloat>using namespace std;// 返回错误码bool hmean(double a, double b, double *ans);bool hmean(double a, double b, double *ans){ if (a == -b) { *ans = DBL_MAX; return false; } else{ *ans = 2.0 * a * b / (a + b); return true; }}void errorCode(){ double x, y, z; cout << "Enter two numbers: "; while (cin >> x >> y) { if (hmean(x, y, &z)) cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; else cout << "One value should not be the negative of the other - try again.\n"; cout << "Enter next set of numbers <q to quit>: "; } cout << "Bye!\n";}int main() { errorCode(); return 0;}
使用throw抛出异常
double hmean(double a, double b){ if (a == -b) { throw "bad hmean() arguments: a = -b not allowed!"; } return 2.0 * a * b / (a + b);}
使用try catch捕获异常
#include <iostream>#include <cfloat>using namespace std;// 捕获异常void errorTryCatch(){ double x, y, z; cout << "Enter two numbers: "; while (cin >> x >> y) { try { z = hmean(x, y); }catch (const char * s) { cout << s << endl; cout << "Enter a new pair of number: "; continue; } cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; cout << "Enter next set of numbers <q to quit>: "; } cout << "Bye!\n";}int main() { errorTryCatch(); return 0;}
将对象用做异常类型
#ifndef PRO1_EXC_MEAN_H#define PRO1_EXC_MEAN_H#include <iostream>using namespace std;class bad_hmean {private: double v1; double v2;public: bad_hmean(double a = 0, double b = 0) : v1(a), v2(b){} void mesg();};inline void bad_hmean::mesg() { cout << "hmean(" << v1 << ", " << v2 << "): " << "invalid argumets: a = -b\n";}class bad_gmean{public: double v1; double v2; bad_gmean(double a = 0, double b = 0) : v1(a), v2(b){} const char *mesg();};inline const char * bad_gmean::mesg() { return "gmean() arguments should be >= 0\n";}#endif //PRO1_EXC_MEAN_H
#include "exc_mean.h"
#include <iostream>#include "module14_error/error_class/exc_mean.h"using namespace std;// function proptotypesdouble hmeans(double a, double b) throw (bad_hmean);double gmeans(double a, double b) throw (bad_gmean);double hmeans(double a, double b) throw (bad_hmean){ if (a == -b) throw bad_hmean(a, b); return 2.0 * a * b / (a + b);}double gmeans(double a, double b) throw (bad_gmean){ if (a < 0 || b < 0) { throw bad_gmean(a, b); } return sqrt(a * b);}void useErrorClass(){ double x, y, z; cout << "Enter two numbers: "; while (cin >> x >> y) { try { z = hmeans(x, y); cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; cout << "Geometric mean of " << x << " and " << y << " is " << gmeans(x, y) << endl; cout << "Enter next set of numbers <q to quit>: "; }catch (bad_hmean & bg) { bg.mesg(); cout << "Try again.\n"; continue; }catch (bad_gmean & hg) { cout << hg.mesg(); cout << "Values used: " << hg.v1 << ", " << hg.v2 << endl; cout << "Sorry , you don't get to play any more.\n"; break; } } cout << "Bye!\n";}int main() { useErrorClass(); return 0;}
堆栈解退
#ifndef PRO1_STACK_RESOLUTION_H#define PRO1_STACK_RESOLUTION_H#include <iostream>#include <cmath>#include <cstring>#include "../error_class/exc_mean.h"using namespace std;class stack_resolution {private: char word[40];public: stack_resolution(const char * str){ strcpy(word, str); cout << "Stack resolution : " << word << " created\n"; } ~stack_resolution(){ cout << "Stack resolution : " << word << " destroyed\n"; } void show()const { cout << "Stack resolution : " << word << " lives!\n"; }};// function prototypesdouble mean_h(double a, double b) throw(bad_hmean);double mean_g(double a, double b) throw(bad_gmean);double mean_s(double a, double b) throw(bad_hmean, bad_gmean);double mean_h(double a, double b) throw(bad_hmean){ if (a == -b) throw bad_hmean(a, b); return 2.0 * a * b / (a + b);}double mean_g(double a, double b) throw(bad_gmean){ if (a < 0 || b < 0) { throw bad_gmean(a, b); } return sqrt(a * b);}double mean_s(double a, double b) throw(bad_hmean, bad_gmean){ double am, hm, gm; stack_resolution d2("found in means()"); am = (a + b) / 2.0; try { hm = mean_h(a, b); gm = mean_g(a, b); } catch (bad_hmean &bg) { bg.mesg(); cout << "Caught in means()\n"; throw ; } d2.show(); return (am + hm + gm) / 3.0;}// 堆栈解退void useStackResolution(){ double x, y, z; stack_resolution d1("found in main()"); cout << "Enter two numbers: "; while (cin >> x >> y) { try { z = mean_s(x, y); cout << "The mean of " << x << " and " << y << " is " << z << endl; cout << "Enter next pair: "; }catch (bad_hmean & bg) { bg.mesg(); cout << "Try again.\n"; continue; }catch (bad_gmean & hg) { cout << hg.mesg(); cout << "Values used: " << hg.v1 << ", " << hg.v2 << endl; cout << "Sorry , you don't get to play any more.\n"; break; } } d1.show(); cout << "Bye!\n";}#endif //PRO1_STACK_RESOLUTION_H
#include "stack_resolution.h"