#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的更多相关文章

  1. Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes

    http://blog.csdn.net/xx326664162/article/details/51859106 总算有个靠谱的了

  2. org.apache.commons.lang.exception包的ExceptionUtils工具类获取getFullStackTrace

    /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreem ...

  3. (转) 将VB.NET网站转换成C#的全过程

    在学习URL重写过程中碰到个是VB写的源码,看起来总是不爽的就GOOLE了下 感觉这个文章写的不错 原文地址 http://www.cnblogs.com/cngunner/archive/2006/ ...

  4. 如何把java代码转换成smali代码

    1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中有许多工具可以把smali代码转化成java代码.但是在学习Smali语法的过程中,有时候需要进行java代码和 ...

  5. 如何通过反射来创建对象?getConstructor()和getDeclaredConstructor()区别?

    1. 通过类对象调用newInstance()方法,适用于无参构造方法: 例如:String.class.newInstance() public class Solution { public st ...

  6. 自己写的粗糙的Excel数据驱动Http接口测试框架(一)

    1.excel用例: 2.用例执行: @Testpublic void BindBank() throws Exception { String fileName = "src/main/j ...

  7. YARN(MapReduce 2)运行MapReduce的过程-源码分析

    这是我的分析,当然查阅书籍和网络.如有什么不对的,请各位批评指正.以下的类有的并不完全,只列出重要的方法. 如要转载,请注上作者以及出处. 一.源码阅读环境 需要安装jdk1.7.0版本及其以上版本, ...

  8. MFC类别概述

    MFC 类别主要可分为下列数大群组: ■ General Purpose classes - 提供字符串类别.数据处理类别(如数组与串行),异 常情况处理类别.文件类别...等等. ■ Windows ...

  9. 网易NAPM Andorid SDK实现原理--转

    原文地址:https://neyoufan.github.io/2017/03/10/android/NAPM%20Android%20SDK/ NAPM 是网易的应用性能管理平台,采用非侵入的方式获 ...

随机推荐

  1. ES6生成器基础

    ES6引进的最令人兴奋的特性就是一种新的函数生成方式,称为生成器(generator).名称有点奇怪,但是第一眼看上去行为更加奇怪.文章主要介绍生成器如何工作,然后让你明白为什么他们对于未来的JS会有 ...

  2. 【MySQL】MySQL锁和隔离级别浅析一

    <MySQL技术内幕InnoDB存储引擎>第一版中对于MySQL的InnoDB引擎锁进行了部分说明,第二版有部分内容更新. 与MySQL自身MyISAM.MSSQL及其他平台BD锁的对比: ...

  3. php xml 互相转换

    正好昨天才做过类似的需求……几行代码就可以搞定. 如果你使用 curl 获取的 xml data$xml = simplexml_load_string($data);$data['tk'] = js ...

  4. leetcode 118

    118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  5. redmine添加自定义属性

    使用redmine创建问题的时候,可能会发现没有我们需要的属性,这时候我们可以添加自定义的属性. 以添加满意度属性为例: 1.进入redmine管理界面,选择自定义属性 2.选择问题下面的新建自定义属 ...

  6. winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变

    在datagridview的EditingControlShowing事件里面添加代码: if (this.dgv_pch.Columns[dgv_pch.CurrentCell.ColumnInde ...

  7. sql 基本操作

    SQL基本操作   一数据类型1整数型 int2精确数值型 decimal(n,p)n为总位数,p为小数位数3浮点型 float4字符型char(n)n最大为4,varchar(n)5日期型datat ...

  8. Vue.js学习 Item5 -- 计算属性computed与$watch

    在模板中绑定表达式是非常便利的,但是它们实际上只用于简单的操作.模板是为了描述视图的结构.在模板中放入太多的逻辑会让模板过重且难以维护.这就是为什么 Vue.js 将绑定表达式限制为一个表达式.如果需 ...

  9. 高可用工具keepalived学习笔记

    keepalived完全遵守VRRP协议包括竞选机制,至于VRRP是什么这里不说了参考http://wenku.baidu.com/link? url=1UbkmHuQlGECgC90P7zF6u2x ...

  10. Jquery数组操作技巧

    Jquery对数组的操作技巧. 1. $.each(array, [callback]) 遍历[常用]  解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不 ...