这是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.

最新文章

  1. 为什么我如此热爱这样一个比赛(转自vici)
  2. HDU 3572 Task Schedule(拆点+最大流dinic)
  3. docker创建镜像的几个命令
  4. div滑入与滑出
  5. 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports
  6. Android 给应用定制皮肤
  7. IP,TCP,UDP Checksum校验
  8. 【LeetCode练习题】Gas Station
  9. 一个简单的webdynpro的ALV示例
  10. mysql导出和导入命令更改数据库名称数据库
  11. 我的MYSQL学习心得(九)
  12. Spring Boot 整合 Redis 实现缓存操作
  13. windows下忘记mysql的root密码解决方法(图文)
  14. 2、各种折腾,安装交叉环境的gcc和qt,测试c++和qt程序
  15. (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布
  16. 从app上传图片到php,再上传到java后端服务器的方法一条龙服务
  17. Ubuntu 13.10 解决虚拟机摄像头无法使用问题
  18. JAVA之经典算法
  19. JUnit 4.0学习笔记
  20. UIPanGestureRecognizer translateInView, locationInView

热门文章

  1. codeforces 667B B. Coat of Anticubism(水题)
  2. [TJOI2012]防御
  3. 洛谷 P1337 平衡点 & bzoj 3680 吊打 XXX —— 模拟退火
  4. vim中编辑了代码 但是提示can not write的解决办法和代码对齐办法
  5. EOJ3263:丽娃河的狼人传说(贪心)
  6. hdoj5806【尺取】
  7. hdoj1260【简单DP】
  8. POJ2446【建图建图】
  9. Cg(C for Graphic)语言语义词与语义绑定详述 (转)
  10. bzoj 4407: 于神之怒加强版【莫比乌斯反演+线性筛】