C++ 异常

调用abort处理异常

  1. #include <iostream>
  2. using namespace std;
  3. // 调用abort处理异常
  4. double hmean(double a, double b){
  5. if (a == -b) {
  6. cout << "untenable argument to hmean()\n";
  7. abort();
  8. }
  9. return 2.0 * a * b / (a + b);
  10. }
  11. void errorOne(){
  12. double x, y, z;
  13. cout << "Enter two numbers: ";
  14. while (cin >> x >> y) {
  15. z = hmean(x, y);
  16. cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
  17. cout << "Enter next set of numbers <q to quit>: ";
  18. }
  19. cout << "Bye!\n";
  20. }
  21. int main() {
  22. errorOne();
  23. return 0;
  24. }

返回错误码

  1. #include <iostream>
  2. #include <cfloat>
  3. using namespace std;
  4. // 返回错误码
  5. bool hmean(double a, double b, double *ans);
  6. bool hmean(double a, double b, double *ans){
  7. if (a == -b) {
  8. *ans = DBL_MAX;
  9. return false;
  10. } else{
  11. *ans = 2.0 * a * b / (a + b);
  12. return true;
  13. }
  14. }
  15. void errorCode(){
  16. double x, y, z;
  17. cout << "Enter two numbers: ";
  18. while (cin >> x >> y) {
  19. if (hmean(x, y, &z))
  20. cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
  21. else
  22. cout << "One value should not be the negative of the other - try again.\n";
  23. cout << "Enter next set of numbers <q to quit>: ";
  24. }
  25. cout << "Bye!\n";
  26. }
  27. int main() {
  28. errorCode();
  29. return 0;
  30. }

使用throw抛出异常

  1. double hmean(double a, double b){
  2. if (a == -b) {
  3. throw "bad hmean() arguments: a = -b not allowed!";
  4. }
  5. return 2.0 * a * b / (a + b);
  6. }

使用try catch捕获异常

  1. #include <iostream>
  2. #include <cfloat>
  3. using namespace std;
  4. // 捕获异常
  5. void errorTryCatch(){
  6. double x, y, z;
  7. cout << "Enter two numbers: ";
  8. while (cin >> x >> y) {
  9. try {
  10. z = hmean(x, y);
  11. }catch (const char * s) {
  12. cout << s << endl;
  13. cout << "Enter a new pair of number: ";
  14. continue;
  15. }
  16. cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
  17. cout << "Enter next set of numbers <q to quit>: ";
  18. }
  19. cout << "Bye!\n";
  20. }
  21. int main() {
  22. errorTryCatch();
  23. return 0;
  24. }

将对象用做异常类型

exc_mean.h

  1. #ifndef PRO1_EXC_MEAN_H
  2. #define PRO1_EXC_MEAN_H
  3. #include <iostream>
  4. using namespace std;
  5. class bad_hmean {
  6. private:
  7. double v1;
  8. double v2;
  9. public:
  10. bad_hmean(double a = 0, double b = 0) : v1(a), v2(b){}
  11. void mesg();
  12. };
  13. inline void bad_hmean::mesg() {
  14. cout << "hmean(" << v1 << ", " << v2 << "): " << "invalid argumets: a = -b\n";
  15. }
  16. class bad_gmean{
  17. public:
  18. double v1;
  19. double v2;
  20. bad_gmean(double a = 0, double b = 0) : v1(a), v2(b){}
  21. const char *mesg();
  22. };
  23. inline const char * bad_gmean::mesg() {
  24. return "gmean() arguments should be >= 0\n";
  25. }
  26. #endif //PRO1_EXC_MEAN_H

exc_mean.cpp

  1. #include "exc_mean.h"

main.cpp

  1. #include <iostream>
  2. #include "module14_error/error_class/exc_mean.h"
  3. using namespace std;
  4. // function proptotypes
  5. double hmeans(double a, double b) throw (bad_hmean);
  6. double gmeans(double a, double b) throw (bad_gmean);
  7. double hmeans(double a, double b) throw (bad_hmean){
  8. if (a == -b)
  9. throw bad_hmean(a, b);
  10. return 2.0 * a * b / (a + b);
  11. }
  12. double gmeans(double a, double b) throw (bad_gmean){
  13. if (a < 0 || b < 0) {
  14. throw bad_gmean(a, b);
  15. }
  16. return sqrt(a * b);
  17. }
  18. void useErrorClass(){
  19. double x, y, z;
  20. cout << "Enter two numbers: ";
  21. while (cin >> x >> y) {
  22. try {
  23. z = hmeans(x, y);
  24. cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
  25. cout << "Geometric mean of " << x << " and " << y << " is " << gmeans(x, y) << endl;
  26. cout << "Enter next set of numbers <q to quit>: ";
  27. }catch (bad_hmean & bg) {
  28. bg.mesg();
  29. cout << "Try again.\n";
  30. continue;
  31. }catch (bad_gmean & hg) {
  32. cout << hg.mesg();
  33. cout << "Values used: " << hg.v1 << ", " << hg.v2 << endl;
  34. cout << "Sorry , you don't get to play any more.\n";
  35. break;
  36. }
  37. }
  38. cout << "Bye!\n";
  39. }
  40. int main() {
  41. useErrorClass();
  42. return 0;
  43. }

堆栈解退

stack_resolution.h

  1. #ifndef PRO1_STACK_RESOLUTION_H
  2. #define PRO1_STACK_RESOLUTION_H
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstring>
  6. #include "../error_class/exc_mean.h"
  7. using namespace std;
  8. class stack_resolution {
  9. private:
  10. char word[40];
  11. public:
  12. stack_resolution(const char * str){
  13. strcpy(word, str);
  14. cout << "Stack resolution : " << word << " created\n";
  15. }
  16. ~stack_resolution(){
  17. cout << "Stack resolution : " << word << " destroyed\n";
  18. }
  19. void show()const {
  20. cout << "Stack resolution : " << word << " lives!\n";
  21. }
  22. };
  23. // function prototypes
  24. double mean_h(double a, double b) throw(bad_hmean);
  25. double mean_g(double a, double b) throw(bad_gmean);
  26. double mean_s(double a, double b) throw(bad_hmean, bad_gmean);
  27. double mean_h(double a, double b) throw(bad_hmean){
  28. if (a == -b)
  29. throw bad_hmean(a, b);
  30. return 2.0 * a * b / (a + b);
  31. }
  32. double mean_g(double a, double b) throw(bad_gmean){
  33. if (a < 0 || b < 0) {
  34. throw bad_gmean(a, b);
  35. }
  36. return sqrt(a * b);
  37. }
  38. double mean_s(double a, double b) throw(bad_hmean, bad_gmean){
  39. double am, hm, gm;
  40. stack_resolution d2("found in means()");
  41. am = (a + b) / 2.0;
  42. try {
  43. hm = mean_h(a, b);
  44. gm = mean_g(a, b);
  45. } catch (bad_hmean &bg) {
  46. bg.mesg();
  47. cout << "Caught in means()\n";
  48. throw ;
  49. }
  50. d2.show();
  51. return (am + hm + gm) / 3.0;
  52. }
  53. // 堆栈解退
  54. void useStackResolution(){
  55. double x, y, z;
  56. stack_resolution d1("found in main()");
  57. cout << "Enter two numbers: ";
  58. while (cin >> x >> y) {
  59. try {
  60. z = mean_s(x, y);
  61. cout << "The mean of " << x << " and " << y << " is " << z << endl;
  62. cout << "Enter next pair: ";
  63. }catch (bad_hmean & bg) {
  64. bg.mesg();
  65. cout << "Try again.\n";
  66. continue;
  67. }catch (bad_gmean & hg) {
  68. cout << hg.mesg();
  69. cout << "Values used: " << hg.v1 << ", " << hg.v2 << endl;
  70. cout << "Sorry , you don't get to play any more.\n";
  71. break;
  72. }
  73. }
  74. d1.show();
  75. cout << "Bye!\n";
  76. }
  77. #endif //PRO1_STACK_RESOLUTION_H

stack_resolution.cpp

  1. #include "stack_resolution.h"