exception -----> Typedefs & Classes
#include <exception>
Typedefs
|
exception_ptr |
一种类型,描述了一个指向异常的指针 |
|
terminate_handler |
一种类型,描述了一个适合作为terminate_handler的函数的指针 |
|
unexperted_handler |
一种类型,描述了一个适合作为unexpected_handler的函数的指针 |
Functions
|
current_exception |
获得当前异常的指针 |
|
get_terminate |
获得当前terminate_handler函数 |
|
get_unexpected |
获得当前unexpected_handler函数 |
|
make_exception_ptr |
创建一个包含exception副本的exception_ptr对象 |
|
rethrow_exception |
抛出一个以参数传递的异常 |
|
set_terminate |
建立一个新的terminate_handler,以便在程序结束时调用 |
|
set_unexpected |
建立一个新的unexpected_handler,以便在程序遇到未知类型异常时调用 |
|
terminate |
调用terminate handler |
|
uncaught_exception |
如果当前正在处理一个已经抛出的异常,那么返回ture |
|
unexpected |
调用一个未知的处理程序 |
Classes
|
bad_exception Class |
描述一种异常,该异常可能从unexpected_handler中抛出 |
|
exception Class |
所有异常类的基类 |
/* ************************************************************************************* */
Typedefs
/* exception_ptr */
typedef unspecified exception_ptr;
说明:
指向exception对象的智能指针类型。这是一种类似shared_ptr的类型:只要还有一个exception_ptr指向exception对象,那么该exception对象就必须保持有效。可以将exception_ptr对象的生命周期延伸到catch语句块外或者不同线程之间。
对于exception_ptr类型,不同的库拥有不同的实现方式,但是其至少需要支持如下几种操作:
- 默认构造函数(接收一个null-pointer)
- 复制构造函数(包括接收一个null-pointer或者nullptr)
- 重载运算符operator==或者operator!=
可以通过以下操作获得exception_ptr对象:current_exception、make_exception_ptr、nested_exception::nested_ptr;通过rethrow_exception重新抛出异常。
// exception_ptr example
#include <iostream> // std::cout
#include <exception> // std::exception_ptr, std::current_exception, std::rethrow_exception
#include <stdexcept> // std::logic_error int main ()
{
std::exception_ptr p;
try
{
throw std::logic_error("some logic_error exception"); // throws
}
catch(const std::exception& e)
{
p = std::current_exception();
std::cout <<"exception caught, but continuing...\n";
} std::cout <<"(after exception)\n"; try
{
std::rethrow_exception(p);
}
catch (const std::exception& e)
{
std::cout <<"exception caught: " << e.what() << '\n';
} return ;
}

/* terminate_handler */
typedef void (*terminate_handler)();
terminate_handler是一个指向void(void)函数的指针,可以用作set_terminate函数的参数和返回值。
/* unexpected_handler */
typedef void (*unexpected_handler)();
unexpected_handler是一个指向void(void)函数的指针,可以用作set_unexpected函数的参数和返回值。
/* ************************************************************************************* */
Classes
/* exception */
class exception {
public:
exception () noexcept;
exception (const exception&) noexcept;
exception& operator= (const exception&) noexcept;
virtual ~exception();
virtual const char* what() const noexcept;
}
说明:
所以标准异常类的基类,因此exception&可以适配所有的异常类型。
直接派生类:
|
bad_alloc |
allocate memory failed |
|
bad_cast |
dynamic case failed |
|
bad_exception |
unexpected handler failed |
|
bad_function_call |
bad call |
|
bad_typeid |
typeid of null pointer |
|
bad_weak_ptr |
bad weak pointer |
|
ios_base::failure |
base class for stream exceptions |
|
logic_error |
logic error |
|
runtime_error |
runtime error |
间接派生类:
|
通过logic_error派生: |
|
|
domain_error |
domain error |
|
future_error |
future error |
|
invalid_argument |
invalid argument |
|
length_error |
length error |
|
out_of_range |
out-of-range |
|
通过runtime_error派生: |
|
|
overflow_error |
overflow error |
|
range_error |
range error |
|
system_error |
system error |
|
underflow_error |
system error |
|
通过bad_alloc派生: |
|
|
bad_array_new_length |
bad array length |
|
通过system_error派生: |
|
|
ios_base::failure |
base class for stream exceptions |
示例代码:
// exception example
#include <iostream> // std::cerr
#include <typeinfo> // operator typeid
#include <exception> // std::exception class Polymorphic
{
virtual void member(){ }
}; int main(){ try
{
Polymorphic * pb = 0;
typeid(*pb); // throws a bad_typeid exception
}
catch(std::exception& e)
{
std::cerr << "exception caught: " << e.what() << '\n';
} return ;
}

/* bad_exception */
class bad_exception : public exception;
说明:
如果bad_exception在函数的throw列表中,那么unexpected将会抛出一个bad_exception来代替terminate函数,因此调用set_unexpected指定的函数后,接着调用bad_exception的catch处理块。
// bad_exception example
#include <iostream> // std::cerr
#include <exception> // std::bad_exception, std::set_unexpected void myunexpected()
{
std::cerr << "unexpected handler called\n";
throw;
} void myfunction() throw(char, std::string, std::bad_exception)
{
throw 100.0; // throws double (not in exception-specification)
} int main(void)
{
std::set_unexpected(myunexpected);
try
{
myfunction();
}
catch(int)
{
std::cerr << "caught int\n";
}
catch(std::bad_exception be)
{
std::cerr << "caught bad_exception: ";
std::cerr << be.what() << "\n";
}
catch(...)
{
std::cerr << "caught some other exception\n";
} return ;
}

上述程序中,语句try{ myfunction(); }后并没有调用terminate()函数,而是先调用set_unexcepted指定的函数myunexpected(),然后调用了
catch(std::bad_exception be)
{
std::cerr << "caught bad_exception: ";
std::cerr << be.what() << "\n";
}
如果throw列表中没有指定std::bad_exception,那么在调用set_unexpected指定的函数后将会调用terminate(),如下所示:

/* nested_exception */
class nested_exception {
public:
nested_exception() noexcept;
nested_exception (const nested_exception&) noexcept = default;
nested_exception& operator= (const nested_exception&) noexcept = default;
virtual ~nested_exception() = default;
[[noreturn]] void rethrow_nested() const;
exception_ptr nested_ptr() const noexcept;
}
说明:
nested exception对象通常可以通过throw_with_nested函数构造,只需要传入outer exception作为参数即可。返回的exception对象拥有与outer exception相同的属性和成员,但是其包含了与nested exception相关的额外信息以及两个用于访问nested exception的成员函数:nested_ptr和rethrow_nested。
// nested_exception example
#include <iostream> // std::cerr
#include <exception> // std::exception, std::throw_with_nested, std::rethrow_if_nested
#include <stdexcept> // std::logic_error // recursively print exception whats:
void print_what(const std::exception& e)
{
std::cerr << e.what() << '\n';
try
{
std::rethrow_if_nested(e);
}
catch(const std::exception& nested)
{
std::cerr << "nested: ";
print_what(nested);
}
} // throws an exception nested in another:
void throw_nested()
{
try
{
throw std::logic_error("first");
}
catch(const std::exception& e)
{
std::throw_with_nested(std::logic_error("second")); /* outer:second; nested:first */
}
} int main()
{
try
{
throw_nested();
}
catch(std::exception& e)
{
print_what(e);
} return ;
} /**
output:
second
nested: first
*/
exception -----> Typedefs & Classes的更多相关文章
- Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes
http://blog.csdn.net/xx326664162/article/details/51859106 总算有个靠谱的了
- org.apache.commons.lang.exception包的ExceptionUtils工具类获取getFullStackTrace
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreem ...
- (转) 将VB.NET网站转换成C#的全过程
在学习URL重写过程中碰到个是VB写的源码,看起来总是不爽的就GOOLE了下 感觉这个文章写的不错 原文地址 http://www.cnblogs.com/cngunner/archive/2006/ ...
- 如何把java代码转换成smali代码
1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中有许多工具可以把smali代码转化成java代码.但是在学习Smali语法的过程中,有时候需要进行java代码和 ...
- 如何通过反射来创建对象?getConstructor()和getDeclaredConstructor()区别?
1. 通过类对象调用newInstance()方法,适用于无参构造方法: 例如:String.class.newInstance() public class Solution { public st ...
- 自己写的粗糙的Excel数据驱动Http接口测试框架(一)
1.excel用例: 2.用例执行: @Testpublic void BindBank() throws Exception { String fileName = "src/main/j ...
- YARN(MapReduce 2)运行MapReduce的过程-源码分析
这是我的分析,当然查阅书籍和网络.如有什么不对的,请各位批评指正.以下的类有的并不完全,只列出重要的方法. 如要转载,请注上作者以及出处. 一.源码阅读环境 需要安装jdk1.7.0版本及其以上版本, ...
- MFC类别概述
MFC 类别主要可分为下列数大群组: ■ General Purpose classes - 提供字符串类别.数据处理类别(如数组与串行),异 常情况处理类别.文件类别...等等. ■ Windows ...
- 网易NAPM Andorid SDK实现原理--转
原文地址:https://neyoufan.github.io/2017/03/10/android/NAPM%20Android%20SDK/ NAPM 是网易的应用性能管理平台,采用非侵入的方式获 ...
随机推荐
- Android控件大全(三)——RecyclerView
是时候用RecyclerView来替换ListView和GridView了 好处就不多说了,百度一搜一大把,来介绍下用法 先定义个适配器: public class BottomSheetAdapte ...
- Redis 1:简介
导读:在今日开讲的项目中,用到了redis数据库.老听大家在说,我都不知道是个啥玩意儿.然后这两天在准备知识分享的事儿,我先大概了解了解,然后讲的时候,能有点共鸣.所以,本篇博客,是在自己跟读完MVA ...
- malloc、calloc、realloc的区别
(1)C语言跟内存分配方式 <1>从静态存储区域分配. 内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...
- 关于Oracle的rac集群和mysql Galera Cluster的想法
到了新公司,公司用的是rac,我比较熟悉mysql第三方的集群方案Galera Cluster这类多主集群, 下面是我参考了他人对rac的介绍,然后和mysql方案进行的臆测级别的分析对比. rac和 ...
- 【Qt 】QSettings写ini配置文件
QSettings写ini配置文件(Qt版本5.2): #include "inidemo.h" #include <QSettings> #include <Q ...
- android studio首次运行出错
转载2015-10-24 16:28:15 标签:androidstudioandroidstudio无法启androidstudio1.4无法 Internal error. Please repo ...
- 【UEditor】远程上传图片到【七牛云存储】
杂谈:最近在玩一个第三方的微信开发平台,里面的图片都是上传到[七牛云存储]的,用了一下非常的好用,支持各种语言,SDK齐全.支持全分布式系统架构以及存储技术和数据加速,于是决定将网站的图片都存储到七牛 ...
- javaSE第十九天
第十九天 227 1:异常(理解) 227 (1) 定义 227 a)异常的引入 227 (2)异常的体系 228 (3)异常的处理: 229 A:JVM的默认处理 ...
- MySQL查询昨天、今天、7天、近30天、本月、上一月数据
文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/291.html 在开发或者统计时,我们可能需要统计某个表的数据.比如:查看今天新增的 ...
- atexit注册的函数是在main函数之后执行?
跟atexit函数相识已久,man手册里对atexit的解释是这么一段: The atexit() function registers the given function to be called ...