How exception works ?

这是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.
时间: 2024-10-12 16:08:20

How exception works ?的相关文章

实现一个简陋操作系统的相关笔记日志

2015年 01月 22日 星期四 16:48:52 CST (vi的`:r!`命令真心好用,可将外部命令的执行结果插入文字中来)今天乘着将近两个月的寒假将于渊的<一个操作系统的实现>这本书看一遍,在刚过去的半年里刚开始时看了前两章,但由于看到保护模式那一块时感到困难也没了心情,因此搁置了.现在操作系统原理和计算机组成已粗略学过了,是时候照着这本书实践一下了.$表示当前行被汇编后的地址$$表示一个section的开始被汇编后的地址.BIOS会将引导扇区中的内容加载到内存,起始地址为0000:7

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

多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessages = new Task(ShowMessages)) { try { taskShowMessages.Start(); DoWorks(); } catch (DoWorkException ex) { Console.WriteLine("Error:{0}", ex.Messag

[转载]A Crash Course on the Depths of Win32 Structured Exception Handling

转自:[已完工][经典文章翻译]A Crash Course on the Depths of Win32 Structured Exception Handling 原文题目: <<A Crash Course on the Depths of Win32™ Structured Exception Handling>> 原文地址: http://www.microsoft.com/msj/0197/Exception/Exception.aspx 原作者: Matt Pietr

C++ 之 exception

本文讲关于C++的异常的所有东西: 绝对不让异常逃离析构函数 阻止exception逃离析构函数,主要是两个原因: 1 防止在异常处理过程中的栈展开行为时,将调用terminate函数.程序将会结束,有时候其实错误并没有那么严重. [插入: 什么时候会调用terminate函数呢?] [回答 : By default, the terminate handler calls abort. But this behavior can be redefined by calling set_term

how tomcat works 读书笔记四 tomcat的默认连接器

其实在第三章,就已经有了连接器的样子了,不过那只是一个学习工具,在这一章我们会开始分析tomcat4里面的默认连接器. 连接器 Tomcat连接器必须满足以下几个要求 1 实现org.apache.cataline.Connector接口 2 负责创建实现了org.apache.cataline.Request接口的request对象 3 负责创建实现了org.apache.cataline.Response接口的response对象 这里默认的连接器的原理很简单,就是等待http请求,创建re

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

You should autowire interface AbstractManager instead of class MailManager. If you have different implemetations of AbstractManager you can write @Component("mailService") and then @Autowired @Qualifier("mailService") combination to au

Hadoop Pipes Exception: Illegal text protocol command

对于Hadoop pipes 出现这样的错误,基本上编译代码依赖的.so和.a 版本不匹配 网上也没有给出更多信息,我的同事最近回复了解决办法,可以参考 https://groups.google.com/a/cloudera.org/forum/#!msg/cdh-user/j0dpYPDx3_A/S1rotrWGSf0J 如果不能翻墙,摘录如下 I met with the same issue, and my way to work around it is to apply the at

How `new’ operator works ?

这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15 February 15, 2013 How `new’ operator works ? Filed under: c++ — Tags: C++ internal, c++ memory layout, c++ new, POD, virtual class — Raison @ 12:38 am (original works by Peixu Zhu) For s

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

上一节说到StandardService负责启动其子组件:container和connector,不过注意,是有先后顺序的,先启动container,再启动connector,这一节先来看看container. 目录 Pipeline和Vavle StandardEngine类和StandardHost类 StandardContext类 总结 Pipeline和Vavle 在第二节(How Tomcat works — 二.tomcat启动(1))中没有介绍关于Pipeline和Vavle,因