/* current_exception */

exception_ptr current_exception() noexcept;

返回指向当前异常(或其副本)的智能指针【具体返回对象本身还是副本,是由具体实现库决定的】,如果当前没有异常发生,那么返回一个null-pointer。exception_ptr是一种shared smart pointer类型:只要仍然有一个exception_ptr指向它,那么被指向的exception对象必须保持有效状态,因此,可以利用exception_ptr跨线程间处理异常。

current_exception函数不抛出异常,但是如果实现该函数时,返回的是指向当前副本的指针,那么如果分配内存失败或者复制副本过程失败,将返回一个bad_exception或者一些未定义的值。

/* get_terminate */

terminate_handler get_terminate() noexcept;

返回终止处理函数。当没有catch语句块可以匹配时,自动调用该函数终止程序的执行。如果之前系统中没有通过set_terminate函数设置终止处理函数,那么根据不同实现系统可能返回一个abort()或者null-pointer。

/* get_unexpected */

unexpected_handler get_unexpected() noexcept;

当函数抛出throw列表中未声明的异常类型时,系统自动调用unexpected处理函数。如果未指定,那么该函数将返回一个unspecified value。

/* make_exception_ptr */

template <class E>

exception_ptr make_exception_ptr(E e) noexcept;

返回一个指向e的副本的exception_ptr对象。其行为等价于如下:

template <class E> exception_ptr make_exception_ptr (E e) noexcept {

try {

throw e;

} catch(...) {

return current_exception();

}

}

 // make_exception_ptr example
#include <iostream> // std::cout
#include <exception> // std::make_exception_ptr, std::rethrow_exception
#include <stdexcept> // std::logic_error int main()
{
auto p = std::make_exception_ptr(std::logic_error("logic_error")); try
{
std::rethrow_exception (p);
}
catch(const std::exception& e)
{
std::cout << "exception caught: " << e.what() << '\n';
} return ;
}

/* rethrow_exception */

[[noreturn]] void rethrow_exception(exception_ptr p);

抛出p所指的异常对象。此时参数p不能为null exception_ptr,否则将引起未定义的行为。

/* set_terminate */

terminate_handler set_terminate(terminate_handler f) noexcept;

将f设置为终止处理函数。如果没有调用该函数设置f,那么系统在适当时候会调用abort()。程序中可以通过调用terminate()来显式调用当前的终止处理函数,即显式调用f或者abort()。

该函数不抛出异常,如果f是无效的或者没有被正确的实现,那么将引起未定义的行为。

 // set_terminate example
#include <iostream> // std::cerr
#include <exception> // std::set_terminate
#include <cstdlib> // std::abort void myterminate()
{
std::cerr << "terminate handler called\n";
abort(); // forces abnormal termination
} int main()
{
std::set_terminate(myterminate);
throw ; // unhandled exception: calls terminate handler return ;
}


/* set_unexpected */

unexpected_handler set_unexpected(unexpected_handler f) noexcept;

/* terminate */

[[noreturn]]void terminate() noexcept;

调用当前终止处理函数。默认情况下调用abort(),但是也可以通过set_terminate()函数来指定。

 // terminate example
#include <iostream> // std::cout, std::cerr
#include <exception> // std::exception, std::terminate int main()
{
char* p, *p2;
std::cout << "Attempting to allocate 2 GB at the same point ...";
try
{
p = new char[1024*1024*1024];
12 p2 = new char[1024*1024*1024];
}
catch (std::exception& e)
{
std::cerr << "ERROR: could not allocate storage\n";
std::terminate();
}
std::cout << "Ok\n"; delete[] p2;
delete[] p;
return ;
}


/* uncaught_exception */

bool uncaught_exception() noexcept;

如果已经抛出了异常,但是还没有被合适的catch语句块处理,则返回true,否则返回false。

/* unexpected */

[[noreturn]] void unexpected();

调用当前unexpected处理函数。默认情况下调用terminate()。但是可以通过set_unexpected()来指定。

/* throw_with_nested */

[[noreturn]] template <class T>

void throw_with_nested(T&& e);

抛出一个联合了当前异常及指定e的嵌套异常。当前异常变为nested exception,而指定e变为outer exception。

exception -----> Functions的更多相关文章

  1. Add GUI to connect to SQL

    (*********************************************************************************) (* *) (* Below i ...

  2. [Python Essential Reference, Fourth Edition (2009)]读书笔记

    Python programs are executed by an interpreter. When you use Python interactively, the special varia ...

  3. python3.4 build in functions from 官方文档 翻译中

    2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...

  4. 5.24 Declaring Attributes of Functions【转】

    转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes o ...

  5. Functions

    Small The first rule of functions is that they should be small.The second rule of functions is that ...

  6. Error Handling and Exception

    The default error handling in PHP is very simple.An error message with filename, line number and a m ...

  7. Think Python - Chapter 16 - Classes and functions

    16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...

  8. Think Python - Chapter 03 - Functions

    3.1 Function callsIn the context of programming, a function is a named sequence of statements that p ...

  9. exception -----> Typedefs & Classes

    #include <exception> Typedefs exception_ptr 一种类型,描述了一个指向异常的指针 terminate_handler 一种类型,描述了一个适合作为 ...

随机推荐

  1. MySQL版本调研

    1引言 1.1 编写目的 本文的主要目的是通过对当前项目中使用的各种版本的数据库进行比较,分析各自特性和稳定程度,最终推荐合适的版本作为今后的标准数据库. 1.2 背景 当前,部门负责管理维护的现网使 ...

  2. Android——Runtime类中的freeMemory,totalMemory,maxMemory等几个方法

    maxMemory() 这个方法返回的是java虚拟机(这个进程)能构从操作系统那里挖到的最大的内存,以字节为单位,如果在运行java程序的时 候,没有添加-Xmx参数,那么就是64兆,也就是说max ...

  3. 【WCF 1】WCF框架宏观了解

    导读:使用WCF框架爱开发项目也有很长一段时间了,最开始的时候,是理解的不深,所以不写博客进行总结.后来是项目赶,发现需要总结的有很多,一直没有把WCF排上日程,尤其是最近研究EF这一块,更是研究了一 ...

  4. .Net 乱序方法

    前两天开发一个奇葩的功能,突然间想到了用打乱顺序的方式解决.记录代码如下: /// <summary> /// 把收集回来的列表打乱顺序之后返回 /// </summary> ...

  5. 使用py2exe打包你的py程序

    软件环境:python3.3.4 + PyQt5 使用py2exe打包写好的py文件,过程如下: 在你要打包的代码文件(比如sample.py)的同文件夹建立一个python代码文件(比如setup. ...

  6. Sublime Text 2/3安装CTags实现函数跳转

    安装ctags 下载 ctags程序,放到目录D:/ctags/下 安装ctags插件 1. 打开Sublime Text 2. Preferences->Package Control-> ...

  7. 一个Brushes笔画应用ios源码完整版

    效果很不错的画笔App,提供几十种不同的画笔,包括毛笔画笔.拥有绘画App的一些基本功能,包括颜色选择.绘画保存,支持撤销(undo).重做(redo)以及橡皮擦功能等等,甚至还支持类似PhotoSh ...

  8. OpenFileDialog使用方法

    OpenFileDialog基本属性 AddExtension 控制是否将扩展名自动添加到文件名上 CheckFileExists 指示用户指定不存在的文件时是否显示警告 CheckPathExist ...

  9. centos6.7下编译安装lnmp

    很多步骤不说明了,请参照本人的centos6.7下编译安装lamp,这次的架构是nginx+php-fpm一台服务器,mysql一台服务器 (1)首先编译安装nginx: 操作命令: yum -y g ...

  10. js浏览器各种位置检测

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...