1 继承图示

2 具体讲解

C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下:

namespace std {

class exception {

public:

exception() throw();   //不抛出任何异常

exception(const exception& e) throw();

exception& operator= (const exception& e) throw();

virtual ~exception() throw)();

virtual const char* what() const throw(); //返回异常的描述信息

};

}

除了exception类,C++还提供了一些类,用于报告程序不正常的情况,在这些预定义的类中反映的错误模型中,主要包含逻辑错误和运行时错误两大类。

逻辑错误主要包括invalid_argument, out_of_range, length_error, domain_error。当函数接收到无效的实参,会抛出invaild_argument异常,如果函数接收到超出期望范围的实参,会抛出out_of_range异常,等等。

namespace std {

class logic_error: public exception {

public:

explicit logic_error(const string &what_arg);

};

class invalid_argument: public logic_error {

public:

explicit invalid_argument(const string &what_arg);

};

class out_of_range: public logic_error {

public:

explicit out_of_range(const string &what_arg);

};

class length_error: public logic_error {

public:

explicit length_error(const string &what_arg);

};

class domain_error: public logic_error {

public:

explicit domain_error(const string &what_arg);

};

}

运行时错误由程序域之外的事件引发,只有在运行时才能检测,主要包括range_error, overflow_error, underflow_error。函数可以通过抛出range_eroor报告算术运算中的范围错误,通过抛出overflow_error报告溢出错误。

namespace std {

class runtime_error: public exception {

public:

explicit runtime_error(const string &what_arg);

};

class range_error: public runtime_error {

public:

explicit range_error(const string &what_arg);

};

class overflow_error: public runtime_error {

public:

explicit overflow_error(const string &what_arg);

};

class underflow_error: public runtime_error {

public:

explicit underflow_error(const string &what_arg);

};

}

另外,在new头文件中定义了bad_alloc异常,exception也是bad_alloc的基类,用于报告new操作符不能正确分配内存的情形。当dynamic_cast失败时,程序会抛出bad_cast异常类,其也继承自exception类。

转载自:http://blog.csdn.net/zhq651/article/details/8425579

3  例子

#include<stdexcept>

try

  {

  throw invalid_argument("hah");

  //throw out_of_range(" we get the outofrange error");

   }catch(invalid_argument& in)

  {

  cout<<"we catch invalid_argument"<<in.what()<<endl;

  }catch(...)

  {

  cout<<"we catch unexpected error,exit"<<endl;

  return 1;

  }

最新文章

  1. web 前端常用组件【02】Select 下拉框
  2. SQL优化方案
  3. Uploadify文件上传
  4. bzoj 1879 [Sdoi2009]Bill的挑战(状压DP)
  5. Python新手学习基础之数据结构-列表1
  6. HDU_1241——石油探索DFS
  7. Mysql怎样删除以“#sql-”开头的暂时表
  8. AR入门系列-在vuforia官网的使用-01-史上最详细AR入门教程
  9. web从入门开始(1)------简介
  10. Luogu1074靶形数独【启发式搜索】
  11. java发送邮件时遇到的坑
  12. strut2接收参数的三种方式
  13. FineUIMvc新特性速递(大间距模式,隐藏菜单垂直滚动条)
  14. getopt实现传参自动识别
  15. VMware同时使用三种网络模式的虚拟机,测试连通性
  16. 论Java的重要性
  17. vue使用百度地图
  18. python学习之老男孩python全栈第九期_day025知识点总结——接口类、抽象类、多态、封装
  19. CF161D Distance in Tree
  20. Highcharts 基本曲线图;Highcharts 带有数据标签曲线图表;Highcharts 异步加载数据曲线图表

热门文章

  1. js的各种错误类型
  2. oracle中触发器的讲解
  3. IO流的操作规律
  4. wifi 3G 流量
  5. Hbase shell 中能否通过filter实现的高级查询
  6. cocos2d CCLOG格式符号表
  7. Mesos架构简介
  8. Day05_JAVAEE系列:XML
  9. PHPer转战Android的学习过程以及Android学习
  10. 用DIV+CSS做网页里要设置body和*规定内容