#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. ASPxCallback控件

    ASPxCallback控件简单来的来说是一个数据回调控件,即不刷新事个页面来展现数据,主要是通过注册客户端事件与服务器端的事件来相互通信完成任务. 如何使用ASPXCallback: 向页面添加Ca ...

  2. a mystrious max subquence sum

    #include<cstdio>#include<cstring>const int maxn=100005;int buf[maxn];int main(){ freopen ...

  3. Spring 注解实体类中非数据库字段属性

    解决办法:在属性的get方法上加上一段注解标识它是临时属性,不是数据库字段就OK @Transient public List<Reverts> getChildList() { retu ...

  4. java 进制相互转换

    public class test{ public static void main(String[]args){ //十进制转二进制. public static void toBin(int nu ...

  5. NSString常见用法总结

    //====================NSStirng 的常见用法==================== -(void)testString { //创建格式化字符串:占位符(由一个%加一个字 ...

  6. iOS 层层推进实现代理模式

    1.代理模式核心思想:A类委托B类做某件事,然后A类获取B类的执行的返回结果! 举例:女孩想去买电影票,但是自己不亲自去而是委托男孩了解电影电影票信息,同时女孩获得男孩买票的结果,代码模拟实现: /* ...

  7. C# 集合 — Hashtable 线程安全

    基础知识重要吗?真的很重要. 就在笔者与同事聊天中突然同事提出一个问题,让笔都有点乱了手脚(有点夸张),题目是这样的: 问:Hashtable 是线程安全的吗? 答:…… (沉默中,Yes Or No ...

  8. CentOS学习笔记--vi程序编辑器

    vi程序编辑器 Linux里经常需要修改一些配置文件,这时就需要一个编辑器,几乎所有的Linux版本都提供了vi这个编辑器. 文件内容查阅cat命令 如果我们要查阅一个文件的内容时,该如何是好呢?这里 ...

  9. JqueryMobile动态生成listView并实现刷新的两种方法

    本篇文章主要是对JqueryMobile动态生成listView并实现刷新的两种方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 JqueryMobile动态生成listView并实现刷新 ...

  10. Session为null 问题

    问题描述: var svode=HttpContext.Current.Session["VCode"].ToString(); //调试时候发现 svode ==null // ...