C++ 标准库中的异常
来源:互联网 发布:瓦赫宁根大学 知乎 编辑:程序博客网 时间:2024/06/05 01:10
1. 相关定义
标准库 c++ 异常类基类 std::exception 定义在文件 exception 中。
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
};
还有两个比较重要的子类是 std::logic_error,std::runtime_error,定义在文件 stdexcept中。
class logic_error : public exception
{
string _M_msg;
public:
/** Takes a character string describing the error. */
explicit logic_error(const string& __arg);
virtual ~logic_error() throw();
/** Returns a C-style character string describing the general cause of
* the current error (the same string passed to the ctor). */
virtual const char* what() const throw();
};
定义后面都附带了 throw(),表示可以抛出任何异常。由定义可以看出,异常子类已经
采用std::string来作为异常内容输入了。
2. 应用举例
//主程序文件
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;
class CMyEXception :public std::logic_error
{
private:
const std::string m_msg;
const int m_errno;
public:
explicit CMyEXception(const std::string &__arg,int ierrno = 0):logic_error(__arg),m_msg(__arg),m_errno(ierrno)
{
//
}
virtual ~CMyEXception() throw()
{
stdf::cout<<"~CMyEXception()"<<endl;
}
virtual const char* what() const throw()
{
//如有必要,覆盖基类的定义
return m_msg.c_str();
}
int geterrno()
{
return m_errno;
}
};
int main()
{
try
{
throw CMyEXception(std::string("dddd"),10);
std::cout<<"after throw,not execute"<<endl;
}
catch(const CMyEXception &e)
{
std::cout<<"CMyEXception:"<<e.what()<<endl;
}
catch(const std::range_error &e)
{
std::cout<<"range_error:"<<e.what()<<endl;
}
catch(const std::runtime_error &e)
{
std::cout<<"runtime_error:"<<e.what()<<endl;
}
catch(const std::exception &e)
{
std::cout<<"exception:"<<e.what()<<endl;
}
catch(...)
{
std::cout<<"unexception ...:"<<endl;
}
}
程序从main开始执行,构造异常类,抛出异常CMyEXception。
异常抛出后,异常后面的输出不会被执行。异常捕获的层次是先按
最匹配的开始,也就是异常类自身,随后是其直接父类,一直上溯
到最上层的父类,这里,首先被 CMyEXception 捕获,输出。
如果没有设置CMyEXception来捕获,那接着就应该是std::runtime_error,
最后是std::exception。程序最后要回到构造异常类的执行点,析构异常类。
如果捕获异常没有采用按应用调用,还要考虑通过子类构造一个父类的拷贝构造过程。
最后,异常调用栈退栈完成,调用才结束。
- C++ 标准库中的异常
- C++ 标准库中的异常
- C++ 标准库中的异常
- 标准C++库中的异常类
- UNIX中的C标准库
- C++中的标准异常类
- C标准库中的时间函数
- 详解C标准库中的atexit()函数
- 浅谈C++ 标准库中的异常 —— stdexcept类
- 标准异常库
- [C++]详解异常处理(Exception Handling) 及标准库异常处理类
- c#中的异常处理
- Objective-C中的异常
- C语言中的异常
- C++中的标准模板库STL及容器
- [转]C标准库中的setjmp与longjmp
- 备忘——C标准库中的qsort和bsearch
- C语言_标准库中的流函数
- 什么是计算机端口?主要有什么用?怎样设置?
- 下载android4.0源码时出错的解决
- Android学习笔记(五一):服务Service(上)- IntentService
- setuptools 安装 (windows)
- iphone 输入/输出流异步读写数据
- C++ 标准库中的异常
- 0xcdcdcdcd ,0xdddddddd ,0xfeeefeee ,0xcccccccc ,0xabababab
- 如何把彩色图片变成灰度图片-GDI+
- error: stdio.h: 没有那个文件或目录
- Mobile web app 开发总结
- EX_NET团队产品介绍-----停车场车位引导系统概述
- 质量保证
- JS的最佳位置
- SEH 与minidump