How exception works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=28
February 18, 2013
How exception works ?
(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 ?的更多相关文章
- Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)
Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件) 前文介绍了通过 Safari 的 Web检查器,可以看到控制台输出的信息.但有时这样调试代码不太方便,如果在 ...
- (C#) Tasks 中的异常处理(Exception Handling.)
多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...
- How Tomcat works — 四、tomcat启动(3)
上一节说到StandardService负责启动其子组件:container和connector,不过注意,是有先后顺序的,先启动container,再启动connector,这一节先来看看conta ...
- Hadoop Pipes Exception: Illegal text protocol command
Hadoop Pipes Exception: Illegal text protocol command 对于Hadoop pipes 出现这样的错误,基本上编译代码依赖的.so和.a 版本不匹配 ...
- How Tomcat Works(十八)
在前面的文章中,如果我们要启动tomcat容器,我们需要使用Bootstrap类来实例化连接器.servlet容器.Wrapper实例和其他组件,然后调用各个对象的set方法将它们关联起来:这种配置应 ...
- How Tomcat Works(十七)
在前面的文章中,已经学会了如何通过实例化一个连接器和容器来获得一个servlet容器,并将连接器和容器相关联:但在前面的文章中只有一个连接器可用,该连接器服务8080端口上的HTTP请求,无法添加另一 ...
- How Tomcat Works(十六)
本文接下来会介绍Host容器和Engine容器,在tomcat的实际部署中,总是会使用一个Host容器:本文介绍Host接口和Engine接口及其相关类 Host容器是org.apache.catal ...
- How Tomcat Works(十四)
我们已经知道,在tomcat中有四种类型的servlet容器,分别为Engine.Host.Context 和Wrapper,本文接下来对tomcat中Wrapper接口的标准实现进行说明. 对于每个 ...
- How Tomcat Works(十二)
tomcat容器通过一个称为Session管理器的组件来管理建立的Session对象,该组件由org.apache.catalina.Manager接口表示:Session管理器必须与一个Contex ...
随机推荐
- 【原】WPF客户端只能启动一次
public partial class App : Application { System.Threading.Mutex mutex; public App() { this.Startup + ...
- java 后台的学习步骤
一.JavaWeb部分 第一阶段:JavaWeb前端技术 web前端技术 HTML, CSS, JavaScript基础, jQuery基础, BootStrap. 第二阶段:服务器端技术 Mysql ...
- 在msys里进行复制和粘贴操作
You can copy text from an MSYS window to the clipboard simply by selecting the text you want to copy ...
- 读取文件中的每行数据,并且存入到list中
有一个txt文件,每行都有数据,将每行的数据转换成list列表 例如: 5,6,7,8,1 9,1,3,4 如下实现: f = open('test1.txt','r') for i in f.rea ...
- centos7命令行模式安装&&配置_br0+kvm+虚拟机+添加硬盘+快照及恢复
KVM创建虚拟机步骤 Submitted by zhaoley on October 18, 2016 - 10:43am 测试环境: 1: 43.243.130.89, CentOS Linux r ...
- Java调用Static修饰的本类方法
public class Dy { public static void main(String[] args){ int a=6; int b=5; int result=0; result=Add ...
- 怎么在notepad里面,将字符串替换成换行
用Notepad++可以,利用查找和替换功能,选择正则表达式,查找目标框里输入你想要替换的字符串,替换为框里输入\r,点击替换即可.这是个 很强大的功能,利用还它可以批量替换任何字符串,比如你链接是& ...
- F - Candy Bags
A. Candy Bags time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- ConcurrentHashMap并不是完全的线程安全
ConcurrentHashMap通过分段锁的方式实现了高效率的线程安全,但是它能否在所有高并发场景中都能保证线程安全呢? public class TestClass { private Concu ...
- UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...