exception -----> Functions

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

时间: 2024-10-13 07:24:24

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

Add GUI to connect to SQL

(*********************************************************************************) (* *) (* Below is the list of support classes that can be used from within the Pascal *) (* script. There are also three support objects available: MainForm of type *

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

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

iOS 基础函数解析 - Foundation Functions Reference

Foundation Functions Reference Framework Foundation/Foundation.h Declared in NSBundle.h NSByteOrder.h NSDecimal.h NSException.h NSObjCRuntime.h NSObject.h NSPathUtilities.h NSRange.h NSZone.h Overview This chapter describes the functions and function

[Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Exception Handling in groovy

def x = new String[3] x[0] = "A" x[1] = "B" x[2] = "C" log.info"XXXXXX 1" try{ x[3] = "D" // def z=9/0 }catch(Exception e){ log.info "Some error "+e.getMessage() // Use e.getMessage() to print ex

OD: Memory Attach Technology - Exception

看到第六章了:形形色色的内存攻击技术 异常处理结构体 S.E.H Structure Exception Handler S.E.H 是 Windows 处理异常的重要数据结构.每个 S.E.H 为 8 字节:包含 S.E.H 链表指针和异常处理函数句柄(两个 DWORD). 1. S.E.H 存放在系统栈中,栈中一般会同时存放多个 S.E.H 2. 线程初始化时,会自动向栈中安装一个 S.E.H,作为线程默认的异常处理. 3. 如果程序源码中使用了 __try{}__except{} 或者 a

解决Exception: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z

在项目中添加src中添加NativeIO类 /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF lice

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 system properties, thread control, networking, object serialization and much more for the running application. Such restrictions may be crucial(重要的:决定性的:定局

exception -----&gt; Typedefs &amp; Classes

#include <exception> Typedefs exception_ptr 一种类型,描述了一个指向异常的指针 terminate_handler 一种类型,描述了一个适合作为terminate_handler的函数的指针 unexperted_handler 一种类型,描述了一个适合作为unexpected_handler的函数的指针 Functions current_exception 获得当前异常的指针 get_terminate 获得当前terminate_handler

SaveData Functions

Here are some save function for some situations: Yes/No /// <summary> /// /// </summary> /// <param name="ds"></param> public void SaveDataSet(DataSet ds, string parent, string child) { string message = "Do you want