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。

 

 

 

 

 

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: Span<T> and universal memory management Span<T>和统一内存管理

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

  3. Memory Management in Open Cascade

    Open Cascade中的内存管理 Memory Management in Open Cascade eryar@163.com 一.C++中的内存管理 Memory Management in ...

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

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

  5. Objective-C Memory Management

    Objective-C Memory Management Using Reference Counting 每一个从NSObject派生的对象都继承了对应的内存管理的行为.这些类的内部存在一个称为r ...

  6. Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm

    目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...

  7. Android内存管理(2)HUNTING YOUR LEAKS: MEMORY MANAGEMENT IN ANDROID PART 2

    from: http://www.raizlabs.com/dev/2014/04/hunting-your-leaks-memory-management-in-android-part-2-of- ...

  8. Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1

    from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...

  9. Understanding Memory Management(2)

    Understanding Memory Management Memory management is the process of allocating new objects and remov ...

随机推荐

  1. [Tue, 11 Aug 2015 ~ Mon, 17 Aug 2015] Deep Learning in arxiv

    Image Representations and New Domains inNeural Image Captioning we find that a state-of-theart neura ...

  2. linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案

    linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案 今天在安装程序时,发现有一个插件未安装,我就随手敲了一个命令,看都 ...

  3. Webservice(CXF) 、 POI(excel)操作部署到weblogic上冲突解决

    这几日把webservice和POI 操作部署到WebLogic上,问题重重,有各种冲突. 部署到tomcat上没有问题 版本: jdk:6 tomcat:6 weblogic:10.3.3 cxf: ...

  4. VS2010 根据WSDL文件(java Web Service)生成.cs文件

    我们添加webService引用,一般是通过 添加服务引用完成的,其实 添加服务引用 在背后为我们生成了代理类. 我们手动生成代理类方法: 1.通过java Web Service,生成wsdl文件: ...

  5. 8088汇编跳转和PSW状态字寄存器

    DDD 8088 汇编跳转 日期:2003年6月12日  出处:嬴政天下整理收藏  作者:看雪  人气: 8735 8088 汇编跳转 一.状态寄存器 PSW(Program Flag)程序状态字寄存 ...

  6. 移动端和PC端有什么区别

    1.PC考虑的是浏览器的兼容性,而移动端开发考虑的更多的是手机兼容性,因为目前不管是android手机还是ios手机,一般浏览器使用的都是webkit内核,所以说做移动端开发,更多考虑的应该是手机分辨 ...

  7. I.MX6 mkuserimg.sh 使用

    /*********************************************************************** * I.MX6 mkuserimg.sh 使用 * 说 ...

  8. linux 远程杀掉进程

    转自http://blog.csdn.net/tengdazhang770960436/article/details/53906263 第一步:获取进程号 pid=$(ssh root@$remot ...

  9. MD5 密码加密算法 系统等待

    MD5 密码加密算法 public static String md(String md, String pass) { MessageDigest m; String passok = " ...

  10. 立体渲染 Volumetric Rendering

    基础概念 在3D游戏引擎中,球体.立方体以及所有其它复杂的集合体都是由三角面片组成的.引擎只会渲染物体的表面,比如球体,半透明物体等.整个世界由各种空壳构成. 立体渲染(Volumetric Rend ...