/* 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();

}

}

 1 // make_exception_ptr example
 2 #include <iostream>       // std::cout
 3 #include <exception>      // std::make_exception_ptr, std::rethrow_exception
 4 #include <stdexcept>      // std::logic_error
 5
 6 int main()
 7 {
 8     auto p = std::make_exception_ptr(std::logic_error("logic_error"));
 9
10     try
11     {
12         std::rethrow_exception (p);
13     }
14     catch(const std::exception& e)
15     {
16         std::cout << "exception caught: " << e.what() << '\n';
17     }
18
19     return 0;
20 }

/* 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是无效的或者没有被正确的实现,那么将引起未定义的行为。

 1 // set_terminate example
 2 #include <iostream>       // std::cerr
 3 #include <exception>      // std::set_terminate
 4 #include <cstdlib>        // std::abort
 5
 6 void myterminate()
 7 {
 8   std::cerr << "terminate handler called\n";
 9   abort();  // forces abnormal termination
10 }
11
12 int main()
13 {
14   std::set_terminate(myterminate);
15   throw 0;  // unhandled exception: calls terminate handler
16
17   return 0;
18 }

/* set_unexpected */

unexpected_handler set_unexpected(unexpected_handler f) noexcept;

/* terminate */

[[noreturn]]void terminate() noexcept;

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

 1 // terminate example
 2 #include <iostream>       // std::cout, std::cerr
 3 #include <exception>      // std::exception, std::terminate
 4
 5 int main()
 6 {
 7     char* p, *p2;
 8     std::cout << "Attempting to allocate 2 GB at the same point ...";
 9     try
10     {
11         p = new char[1024*1024*1024];
12         p2 = new char[1024*1024*1024];
13     }
14     catch (std::exception& e)
15     {
16         std::cerr << "ERROR: could not allocate storage\n";
17         std::terminate();
18     }
19     std::cout << "Ok\n";
20
21     delete[] p2;
22     delete[] p;
23     return 0;
24 }

/* 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。

转载于:https://www.cnblogs.com/benxintuzi/p/4617984.html

exception ----- Functions相关推荐

  1. python代码风格指南_记录Python代码:完整指南

    python代码风格指南 Welcome to your complete guide to documenting Python code. Whether you're documenting a ...

  2. Item 39. 异常安全之函数(Exception Safe Functions)

    Item 39. Exception Safe Functions 编写异常安全代码的难点不在于抛出和捕获异常,而是在于抛出和捕获之间要做的事情.当异常从抛出 到达捕获语句的过程中,这期间执行的函数在 ...

  3. JAVA Functions in XI(转)

    1. String [ ] StrArray = LGORT.split(",") //-- pass LGORT to this UDF           int len1 = ...

  4. 判断exception类型_C++核心准则T.44:使用函数模板推断类模板参数类型(如果可能)...

    T.44: Use function templates to deduce class template argument types (where feasible) T.44:使用函数模板推断类 ...

  5. Printed Exception strings - what do all those flags mean?(转)

    Printed Exception strings - what do all those flags mean? 软件调试 2008-09-26 17:10:21 阅读704 评论1 字号:大中小 ...

  6. Aggregate functions cannot be used in the select right after the flatAggregate

    代码如下: Table groupByDistinctResult = orders.window(Tumble.over(lit(5).minutes())                 .on( ...

  7. Azure Functions + Azure Batch实现MP3音频转码方案

    客户需求 客户的环境是一个网络音乐播放系统,根据网络情况提供给手机用户收听各种码率的MP3歌曲,在客户没购买歌曲的情况下提供一个三十秒内的试听版本.这样一个系统非常明确地一个需求就是会定期需要将一批从 ...

  8. Using Java SecurityManager to grant/deny access to system functions

    In Java it is possible to restrict access to specific functions like reading/writing files and syste ...

  9. 最新的Functions 类

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...

最新文章

  1. java继承的生活例子,帮你突破瓶颈
  2. 【译文】AppBarLayout的越界滚动行为
  3. [云炬创业管理笔记]第二章成为创业者讨论1
  4. 行车记录仪设置php,【必看】如何正确的设置“行车记录仪”
  5. 经典蓝牙和低功耗蓝牙(BLE)有什么区别?
  6. 体积最小桌面linux,Tiny Core Linux - 体积最小的精简 Linux 操作系统发行版之一 (仅10多MB)...
  7. [Unity] 战斗系统学习 11:Buff 框架 1
  8. 【Spring】Spring的生态项目
  9. 转:如何把 SQL Server 的自增标志列清零
  10. python类中方法相互调用_python中同一个类,带参的方法直接如何相互调用
  11. LaTeX常用符号与语法
  12. 全志r16android sdk,全志 Allwinner R16 SoC 全套设计资料分享 原理图 PCB 数据手册 SDK...
  13. 用户使用情况报告(附用户使用调查表)
  14. C语言也能干大事第七节(如鹏基础)
  15. win10未启动对远程服务器启动,win10系统连接远程提示未启用对服务器的远程访问的操作技巧...
  16. 手把手教你处理 JS 逆向之图片伪装
  17. PHP的strtotime()
  18. RedHat FC5安装xmms
  19. 今年被裁员三次,还有两个月直接躺平吗?
  20. [书]x86汇编语言:从实模式到保护模式 -- 第13章 mbr加载内核、内核加载应用程序

热门文章

  1. 利用反汇编手段解析C语言函数
  2. 图解Dev C++ 创建Win32 项目模板
  3. Android - Intentservice源码解析
  4. 基于R语言的时间序列分析预测
  5. c# 建立到数据源的连接 以及获取项目配置文件的属性
  6. 5分钟安全顾问 - 针对家庭办公室用户的简单防火墙安装
  7. 神经网络迭代次数与Lambert定律
  8. 为什么要在神经网络分类训练中使用 Cross-Entropy?(to be continued)
  9. TensorFlow 资源汇集
  10. 【Paper】2019_Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D