2012年12月30日

c++ 从标准异常类别(Exception Classes)派生新类别

摘要: (1)直接派生自exception,自己实现what()函数。namespace MyLib { /* user-defined exception class * derived from a standard class for exceptions */ class MyProblem : public std::exception { public: … MyProblem(…) { // special constructor } virtual const char* what() const throw() { // what() function... 阅读全文

posted @ 2012-12-30 14:18 zhuyf87 阅读(492) 评论(0) 推荐(0) 编辑

c++ 抛出标准异常

摘要: 可以在自己的程序中抛出某些标准异常。抛出标准异常时,只需生成一个描述该异常的字符串,交给异常对象,它将成为what()返回的描述字符串。std::string s;…throw std::out_of_range(s);throw std::out_of_range(“out_of_range (somewhere, somehow)”);提供这种功能的标准异常有:logic_error及其派生类别、runtime_error及其派生类别、ios_base::failure。不能抛出exception,也不能抛出任何用以支持语言核心性质的异常(bad_alloc、bad_cast、bad_ty 阅读全文

posted @ 2012-12-30 13:37 zhuyf87 阅读(451) 评论(0) 推荐(0) 编辑

c++ 异常类别(Exception Classes)的成员

摘要: 为了在catch子句中处理异常,需要知道异常提供哪些接口。所有标准异常只提供一个可用的接口函数:what(),用以获取异常“型别之外的附加信息”。namespace std { class exception { public: virtual const char* what() const throw(); … };}what()返回的字符串,很大程度决定了帮助的级别和信息的详细度。该字符串是以null结尾的“multibyte”字符串,也可以轻松转换为wstring并显示出来。what()返回的C-string在其所属的异常对象销毁后,就不再有效(标准没有强制)。除了w... 阅读全文

posted @ 2012-12-30 09:00 zhuyf87 阅读(1803) 评论(0) 推荐(0) 编辑

导航