来自https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html:
@finally — Defines a block of related code that is subsequently executed whether an exception is thrown or not.
但它没有说明 catch 块中的异常。这听起来合乎逻辑,这个例外不会被咳嗽。
我制作了简单的程序来检查:
import <Foundation/Foundation.h>
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init] ;
int result = 0 ;
@try {
@throw [NSException exceptionWithName:@"Exception" reason:@"@try" userInfo:nil];
}
@catch (id exception) {
@throw [NSException exceptionWithName:@"Exception" reason:@"@catch" userInfo:nil];
}
@finally {
NSLog(@"Finally");
}
[pool release] ;
return result ;
}
只需编译并执行:
$ gcc -framework Foundation -fobjc-exceptions test.m
$ ./a.out
2012-12-29 00:39:21.667 a.out[86205:707] *** Terminating app due to uncaught exception 'Exception', reason: '@catch'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8e3050a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8e56e3f0 objc_exception_throw + 43
2 a.out 0x0000000107d48d47 main + 359
3 libdyld.dylib 0x00007fff90b4e7e1 start + 0
)
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6