这是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. ES6 对象的解构赋值

    对象的解构赋值 解构不仅可以用于数组,还可以用于对象. let {foo,bar} = {foo:"aaa",bar:"bbb"}; console.log(f ...

  2. html5--6-67 阶段练习8-弹性三列布局

    html5--6-67 阶段练习8-弹性三列布局 学习要点 运用弹性盒子模型完成一个三列布局,加深对学过知识点的综合应用能力. 了解用百分比设置元素高度的方法. @charset="UTF- ...

  3. WAS:修改jsp编译器用JDK5.0

      问题现象: 今天有现场反映,访问应用的个别页面报错,报错内容如下: 于是先查看其他现场,都是好的:根据报错信息,提示的意思是jsp解析不了. 结合上面2个情况,排除代码问题,应该是现场WAS环境问 ...

  4. perl字符集处理

    本文内容适用于perl 5.8及其以上版本. perl internal form 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种u ...

  5. 【系列】 2-SAT

    bzoj 1997 Planar 题目大意: 给一个存在曼哈顿回路的无向图,求该图是否为平面图 思路: 先把曼哈顿回路提出来,则剩下的边的两个端点若有$ABAB$的形式则这两条边必定一个在环外一个在环 ...

  6. 51nod1163【贪心】

    思路: 我们可以说: ①:价值大的不管时间早晚,都可以取,时间较晚的,本身就可以取,那么肯定是大的在前面取,但是在最前面那也是不对的,那么条件就是在规定的时间内,大的就是取了,因为他大,OK. ②:只 ...

  7. Codeforces626B - Cards【模拟】

    题意: 两张相同可以合并成相同: 两张不同可以产生另外一个不同: 求最终的可能颜色: 思路: 模拟啊. 总共也就那么几种情况,具体看挫code--. #include<iostream> ...

  8. hdoj5806【尺取】

    (补题,妈蛋那时候大哥给我说是尺取,我不想打-真是艾斯比了-) 题意: 退役狗 NanoApe 滚回去学文化课啦! 在数学课上,NanoApe 心痒痒又玩起了数列.他在纸上随便写了一个长度为 n 的数 ...

  9. (12)用css设计电子相册 {下}

    本篇学习资料讲解:        延续上一篇的学习资料,仍然介绍 使用css对电子相册进行排版 和 侧面强调“盒子模型.标准流.浮动和定位”的重要性. 上篇学习资料介绍的“阵列模式电子相册”,如果也能 ...

  10. PJzhang:centos7上LNMP方式安装dvwa漏洞测试环境

    猫宁!!! 参考链接:https://www.jianshu.com/p/5491ce5bfbac https://www.cnblogs.com/wujuntian/p/8183952.html h ...