C++ 异常的详细介绍
副标题[/!--empirenews.page--]
C++ 异常的详解 程序有时会遇到运行阶段错误,导致程序无法正常执行下去。c++异常为处理这种情况提供了一种功能强大的而灵活的工具。异常是相对比较新的C++功能,有些老编译器可能没有实现。另外,有些编译器默认关闭这种特性,我们可能需要使用编译器选项来启用它。 一、异常机制的使用 异常提供了将控制程序的一个部分传递到另一部分的途径。对异常的处理有3个组成部分: 引发异常 示例代码: #include "stdafx.h" #include <iostream> double hmean(double a,double b); int main() { double x,y,z; std::cout << "Enter two numbers: "; while (std::cin >> x >> y) { try { z = hmean(x,y); } catch(const char *s ){ std::cout << s << std::endl; std::cout << " Enter a new pair of numbers: "; continue; } std::cout << "Harmonic mean of " << x << " and " << y << " is " << z << std::endl; std::cout << "Enter next set of numbers <q to quit>: "; } std::cout << "Bye! n"; system("pause"); return 0; } double hmean(double a,double b) { if (a == -b) { throw "bad hmean() arguments a= -b not allowed"; } return 2.0 *a*b / (a + b); } Enter two numbers: 3 6 Harmonic mean of 3 and 6 is 4 Enter next set of numbers <q to quit>: 10 -10 bad hmean() arguments a= -b not allowed Enter a new pair of numbers: q Bye! 请按任意键继续. . . 程序说明: try块: try { z = hmean(x,y); } 引发异常的代码: if (a == -b) { throw "bad hmean() arguments a= -b not allowed"; } 执行throw语句类似于执行返回语句,因为他也将终止函数的执行;但throw不是讲控制权返回给调用程序,而是导致程序沿函数调用序列后退,知道找到包含try块的函数。 处理程序(或catch块): catch(const char *s ){ std::cout << s << std::endl; std::cout << " Enter a new pair of numbers: "; continue; } 二、将对象用作异常类型 通常,引发异常的函数将传递一个对象。这样做的重要优点之一是,可以使用不同的异常类型来区分不同的函数在不同情况下引发的异常。另外,对象可以携带信息,程序员可以根据这些信息来确定引发异常的原因。同时,catch块可以根据这些信息来决定采取什么样的措施。 示例: exc_mean.h #include "stdafx.h" #include <iostream> 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() { std::cout << "hmean ( " << v1 << "," << v2 << ") ;" << "invalid argumnents: a =-b n"; } class bad_gmean { public : double v1; double v2; bad_gmean(double a = 0,v2(b) {} const char * mesg(); }; inline const char* bad_gmean::mesg() { return "gmean() arguments shoud be >=0 n"; } 测试代码: #include "stdafx.h" #include <iostream> #include <cmath> #include "exc_mean.h" double hmean(double a,double b); double gmean(double a,double b); int main() { using std::cout; using std::cin; using std::endl; double x,z; 1 >> 2; cout << "Enter two numbers "; while (cin >> x >> y) { try { z = hmean(x,y); cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; cout << " Geometric mean of " << x << " and " << y << " is " << gmean(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 << "Value used: " << hg.v1 << "," << hg.v2 << endl; cout << "Sorry,you don't get to play any more .n "; break; } } cout << " Bye! n"; system("pause"); return 0; return 0; } double hmean(double a,double b) { if (a == -b) throw bad_hmean(a,b); return 2.0 * a*b / (a + b); } double gmean(double a,double b) { if (a < 0 || b < 0) throw bad_gmean(a,b); return std::sqrt(a * b); } 输出结果: Enter two numbers 4 12 Harmonic mean of 4 and 12 is 6 Geometric mean of 4 and 12 is 6.9282 Enter next set of numbers <q to quit >:5 -5 hmean ( 5,-5) ;invalid argumnents: a =-b Try again. 5 -2 Harmonic mean of 5 and -2 is -6.66667 gmean() arguments shoud be >=0 Value used: 5,-2 Sorry,you don't get to play any more . Bye! 请按任意键继续. . . 三、异常规范 异常规范是C++98的一项功能,但c++11将其摒弃了。这意味着c++11仍然处于标准之中,但以后可能会从标准中剔除,因此不建议使用它。 异常规范示例: double harm(double a ) throw(bad_thing);//可能会抛出 bad_thing异常 double marm(double ) throw() ;//不抛出异常 异常规范的作用: 1、告诉用户可能需要使用try块,然而这项功能也可使用注释轻松完成。 2、让编译器添加执行运行阶段检查代码,检查是否违反了异常规范,然而这很难检查,例如marm可能不会引发异常,但它可能调用一个函数,而这个函数调用另一个函数引发了异常 总之最好不要使用这项功能,c++11也建议忽略异常规范 然而c++11确实支持一种特殊的异常规范,可使用关键字noexcept 例如 double marm() noexcept; 四、栈解退 (编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |