这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=28

February 18, 2013

How exception works ?

Filed under: c++ — Tags: C++ constructor., C++ exception handling, C++ internal — Raison @ 10:23 am

(original works by Peixu Zhu)

1. Throw Exception

  • Set the size of the exception and then call internal routine __cxa_allocate_exception to allocate memory.
  • __cxa_allocate_exception call malloc to allocate memory.  plus 0x78 bytes buffer to install default handlers later. if allocation fails, globally lock and throw an emergency exception, then globally unlock. If allocation successes, the  __cxa_allocate_exception routine return the address of the memory block minus 0x78 bytes.
  • Set the exception data as first argument,   and typeinfo as second argument,  then call internal routine __cxa_throw .
  • In __cxa_throw,  the routine installs default handlers in sequence at the additional memory provided in the first argument. and then unwind the stack  to match and call catch block by calling  __Unwind_RaiseException (in libunwind).

2. Catch block

  • Call dyld_stub___cxa_begin_catch.
  • Perform the blocked code.
  • Call dyld___sub_cxa_end_catch, the allocated exception object will be released.

3. Notes

  • As above, the code in catch{…} block should be strictly scoped in the block, and the exception object should be as simple as possible, any unexpected errors in the exception object will cause unexpected problems, and difficult to locate or debug it.
  • In addition to catching the exceptions thrown in codes, we should also catch default exceptions like std::bad_alloc, since the internal routine may throw such exceptions potentially.
  • It is not suggested to throw exceptions in any constructor methods, for causing unexpected results. If we throw an exception in a constructor method, we’ve in fact get a memory block for the object, though the memory address is not returned to calling routine, thus, the exception handler can not get the address information at all, and can not free the allocated memory explicitly or implicitly. Since the object is not created successfully, the destructor will not be called automatically.

How exception works ?的更多相关文章

  1. Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)

    Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件) 前文介绍了通过 Safari 的 Web检查器,可以看到控制台输出的信息.但有时这样调试代码不太方便,如果在 ...

  2. (C#) Tasks 中的异常处理(Exception Handling.)

    多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...

  3. How Tomcat works — 四、tomcat启动(3)

    上一节说到StandardService负责启动其子组件:container和connector,不过注意,是有先后顺序的,先启动container,再启动connector,这一节先来看看conta ...

  4. Hadoop Pipes Exception: Illegal text protocol command

    Hadoop Pipes Exception: Illegal text protocol command 对于Hadoop pipes 出现这样的错误,基本上编译代码依赖的.so和.a 版本不匹配 ...

  5. How Tomcat Works(十八)

    在前面的文章中,如果我们要启动tomcat容器,我们需要使用Bootstrap类来实例化连接器.servlet容器.Wrapper实例和其他组件,然后调用各个对象的set方法将它们关联起来:这种配置应 ...

  6. How Tomcat Works(十七)

    在前面的文章中,已经学会了如何通过实例化一个连接器和容器来获得一个servlet容器,并将连接器和容器相关联:但在前面的文章中只有一个连接器可用,该连接器服务8080端口上的HTTP请求,无法添加另一 ...

  7. How Tomcat Works(十六)

    本文接下来会介绍Host容器和Engine容器,在tomcat的实际部署中,总是会使用一个Host容器:本文介绍Host接口和Engine接口及其相关类 Host容器是org.apache.catal ...

  8. How Tomcat Works(十四)

    我们已经知道,在tomcat中有四种类型的servlet容器,分别为Engine.Host.Context 和Wrapper,本文接下来对tomcat中Wrapper接口的标准实现进行说明. 对于每个 ...

  9. How Tomcat Works(十二)

    tomcat容器通过一个称为Session管理器的组件来管理建立的Session对象,该组件由org.apache.catalina.Manager接口表示:Session管理器必须与一个Contex ...

随机推荐

  1. kafka 查询 SQL Query

    . SELECT COUNT(*) FROM wiseweb_crawler_foreignmedias WHERE site_id= AND (gathertime BETWEEN '2017-05 ...

  2. 阮一峰 KMP BM算法

    存一个链接,讲得好啊! 点击这里打开     字符串KMP 点击这里打开     字符串匹配的Boyer-Moore算法

  3. 「LuoguP2014」 选课

    Description 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学 ...

  4. CodeBlocks+Qt(MinGW)配置

    1.安装CodeBlocks 官网:http://www.codeblocks.org/ 下载地址:http://www.codeblocks.org/downloads/26 有以下两种选择 cod ...

  5. Bootstrap 网格系统的工作原理

    网格系统通过一系列包含内容的行和列来创建页面布局.下面列出了 Bootstrap 网格系统是如何工作的: 行必须放置在 .container class 内,以便获得适当的对齐(alignment)和 ...

  6. 关于spring boot在启动的时候报错: java.lang.Error: generate operation swagger failed, xxx.xxx.xxx

    Error starting ApplicationContext. To display the auto-configuration report re-run your application ...

  7. Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate

      Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...

  8. js的NaN变量

    js中,我们经常在parseInt函数的时候遇到NaN变量,这个变量到底是什么呢? w3c上这样解释: NaN 属性是代表非数字值的特殊值.该属性用于指示某个值不是数字.可以把 Number 对象设置 ...

  9. bug日志-天坑,Spring Security的登陆报错:An internal error occurred while trying to authenticate the user.

    在学习Spring Security的时候,我的编辑器给我报错:An internal error occurred while trying to authenticate the user. 明明 ...

  10. Ascall码的故事

    没事发个ascall码表,二进制值得研究呦 sub al,30h; and al,00001111b ;字符ascall转数字or al,00110000b; sub al,32; and al,11 ...