Objective-C Memory Management    Being Exceptional  异常处理与内存

3.1Cocoa requires that all exceptions must be of type NSException

cocoa 需要所有的异常是NSException类型的。

so even though you can throw an exception from other objects, Cocoa isn't set up to deal with those.

所以你即使从别的类抛出异常,cocoa 也不会处理。

Exception handling is really intended for errors that are generated by your programs. Cocoa frameworks usually handle errors by exiting the program, which is not what you want. You should throw and catch exceptions in your code, instead of letting them escape to the framework level.

cocoa框架通常用已经存在的程序解决异常。你应该throw and catch exceptions  在你自己的代码中,而不是任其跑到框架层。

To enable support for exceptions, make sure the -fobj-exceptions flag is turned on.

为了支持异常,你应该确保-fobj-exceptions打开。

When an exception is thrown and is not caught, the program stops at the exception point and propagates the exception.

当一个异常抛出后,并没有被接到的话,则程序停在异常点。

3.2  Keywords for exceptions

All the keywords for exceptions start with @. Here's what each one does

所以的异常keyword 均以@开头。

(1)@try: Defines a block of code that will be tested to determine if an exception

should be thrown.

测试一个异常是否应该被抛出。

(2)@catch(): Defines a block of code for handling a thrown exception. Takes an

argument, typically of type NSException, but can be of other types.

处理异常

(3)@finally: Defines a block of code that gets executed whether an exception is

thrown or not. This code will always be executed.

不管是否抛出异常都运行。

(4)@throw: Throws an exception.

抛出异常

3.3 Catching Different types of exceptions

You can have multiple @catch blocks depending on which type of exception you want to handle.

可以根据exception 的类型来选择@catch 的类型

@try{

} @catch (MyCustomException *custom) {

} @catch (NSException *exception) {

} @catch (id value) {

} @finally {

}

A program throws an exception by creating an instance of NSException and using one of two techniques:

一个程序抛出异常通过创建NSException 和使用下面的技术:

(1)Using @throw exception;

(2)Sending a raise message to an NSException objectwe'll create an exception: 我们建一个NSExcepiton

NSException *theException = [NSException exceptionWithName: ...];

We can then throw with either 我们throw 通过下面:

@throw theException;

or

[theException raise];

You'll usually throw exceptions from inside the exception handling code.

也可以在一个exception handling code里再抛出异常。

@try {

NSException *e = ...;

@throw e; }

@catch (NSException *e) {

@throw; // rethrows e.

}

2.4 Exceptions need memory management too . 异常也需要内存管理

Memory management can be tricky when exceptions are involved.

- (void)mySimpleMethod

{

NSDictionary *dictionary = [[NSDictionary alloc] initWith....];

[self processDictionary:dictionary];

[dictionary release];

}

let's imagine that processDictionary throws an exception. The program jumps out of this method and looks for an exception handler. But because the method exits at this point, the dictionary object is not released, and we have a memory leak.

假如processDictionary 抛出异常,但是dictionary 还没有释放,因此有了memory leaks .

解决办法:One simple way to handle this is to use @try and @finally, doing some cleanup in @finally because it's always executed (as we said earlier).

在@finally 处处理,因为总是执行。

- (void)mySimpleMethod

{

NSDictionary *dictionary = [[NSDictionary alloc] initWith....];

@try {

[self processDictionary:dictionary];

}

@finally {

[dictionary release];

}

}

2.5 Exceptions and autorelease pools   异常和自动释放池

Exceptions are almost always created as autoreleased objects because you don't know when they will need to be released. When the autorelease pool is destroyed, all objects in that pool are destroyed also, including the exception.

Exceptions 几乎总是被创作为自动释放因为你不知道什么时候结束。

- (void)myMethod

{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSDictionary *myDictionary =

[[NSDictionary alloc] initWithObjectsAndKeys:@"asdfads", nil];

@try {

[self processDictionary:myDictionary];

} @catch (NSException *e) {

@throw;

} @finally {

[pool release];

}

}

There's a problem when we think about exception handling. We discussed earlier that we can rethrow exceptions in the @catch block, which causes the @finally block to execute before the exception is rethrown.

我们可以rethrow exceptions 在@catch block中。这导致了@finally在异常抛出前执行。

This will cause the local pool to be released before the exception can be delivered, thus turning it into a dreaded zombie exception.

这将导致恐怖的zombie exception.

- (void)myMethod

{

id savedException = nil;

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSDictionary *myDictionary =

[[NSDictionary alloc] initWithObjectsAndKeys:@"asdfads", nil];

@try {

[self processDictionary:myDictionary];

} @catch (NSException *e) {

savedException = [e retain];

@throw;

} @finally {

[pool release];

[savedException autorelease];

}

}

By using retain, we saved the exception in the parent pool. When our pool is released, we already have a pointer saved, and when the parent pool is released, the exception will be released with it.

通过retain ,我们保留了这个exception 在parent pool。

转载于:https://www.cnblogs.com/ljlkfx/p/4482336.html

Objective-C Memory Management Being Exceptional 异常处理与内存相关推荐

  1. Objective -C Memory Management 内存管理 第一部分

    Objective -C Memory Management  内存管理  第一部分 Memory management is part of a more general problem in pr ...

  2. [译]C# 7系列,Part 10: Spanlt;Tgt; and universal memory management Spanlt;Tgt;和统一内存管理

    原文:https://blogs.msdn.microsoft.com/mazhou/2018/03/25/c-7-series-part-10-spant-and-universal-memory- ...

  3. ARM异常处理(3):Bus faults、Memory management faults、Usage faults、Hard faults详解

    之前介绍了了ARM异常处理(1):异常类型.优先级分组和异常向量表,里面有很多异常类型,其中有几个异常在错误处理中非常有用: 文章目录 1 Bus Fault 2 Memory Management ...

  4. 异构内存管理 Heterogeneous Memory Management (HMM)

    https://www.kernel.org/doc/html/latest/vm/hmm.html 目录 异构内存管理 (HMM) 使用特定于设备的内存分配器的问题 I/O 总线.设备内存特性 共享 ...

  5. MIT 6.828 (二) Lab 2: Memory management

    Lab 2: Memory management 做这个实验之前首先需要知道什么是分页.分段在这个实验里面没用到过. 前面是一大堆教你怎么获取lab2资源的,我不知道怎么弄,后来乱搞了一下,就把lab ...

  6. Item 36. Class-Specific Memory Management

    Item 36. Class-Specific Memory Management 在类中声明operator new 和 operator delete 成员就可以实现类自己的内存分配与管理. cl ...

  7. [翻译] - Inside SQL Server 2000's Memory Management Facilities

    原文地址:Inside SQL Server 2000's Memory Management Facilities     翻译:RicCC Ken Henderson     Microsoft ...

  8. Operating System Concepts--chap9 Memory Management;

    为什么80%的码农都做不了架构师?>>>    这一章节的memory management内容覆盖从bare-machine approach到paging和segmentatio ...

  9. Java (JVM) Memory Model – Memory Management in Java

    原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...

最新文章

  1. 基于队列的生产消费设计java_生产者-消费者设计模式
  2. 教你用机器学习匹配导师 !(附代码)
  3. react部署之页面空白
  4. 最近在学OpenGL和英语
  5. batch insert 1 million datas into mysql
  6. how tomcat works(第15章: Digester)
  7. PinPhoto On OS X
  8. 如何掌握眼神接触技巧?
  9. Eclipse用法和技巧十九:eclipse修改workspace
  10. php链接没有下划线,html超链接怎么去掉下划线
  11. 2022年美赛C题M奖思路复盘(附代码、附论文)
  12. 白帽黑客郭盛华:供应链枢纽需要从网络安全开始
  13. Spark SQL的自定义函数UDF
  14. 代码打累了看看短腿基!
  15. 当年明月、袁腾飞、阎崇年三人的专业水平的比较以及由此想到的一些学习方法、品书原则
  16. 蓝桥杯——算法基础 逗志芃的暴走PYTHON
  17. CSU 1224 ACM小组的古怪象棋
  18. 中国彻底放弃2G网络仍需十年时间
  19. android10官方刷机包下载,Android 11 喜讯!小米 10 率先尝鲜,官方刷机包发布下载...
  20. 【Java学习从入门到入土,手把手吧基础知识填充进你的大脑】

热门文章

  1. nginx与lighttpd性能简单对比
  2. C++11 新特性整理 (1)
  3. OpenCV-python学习笔记(四)——smoothing and blurring平滑和模糊
  4. python随机数小游戏
  5. 八年级计算机考操作试题,八年级计算机会考必看试题!!!!!!
  6. handler和thread之间如何传输数据_换机必备知识:如何将数据转移到Oppo手机上
  7. 四阶行列式计算_四阶行列式的计算
  8. elasticsearch 文档_ElasticSearch系列04:索引与文档的CURD
  9. A股开盘:深证区块链50指数涨0.94%,*ST晨鑫涨停
  10. SAP License:OB52等与Client状态相关的前台操作