在 Linux 中使用 Objective-C 捕获异常
我有一些示例代码没有按照我认为应该的方式运行。
#import <Foundation/NSString.h>
#import <Foundation/NSException.h>
#import <Foundation/NSAutoreleasePool.h>
#import <stdio.h>
int main( int argc, const char *argv[] ) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
NSException *e = [NSException
exceptionWithName: @"NSException"
reason: @"The level is below 0"
userInfo: nil];
@throw e;
} @catch ( NSException *e ) {
printf( "+%s: ", [[e name] UTF8String] );
}
[pool release];
}
我按如下方式运行它:
> gcc -g -c main.m -fconstant-string-class=NSConstantString -I/usr/include/GNUstep
> gcc main.o -lgnustep-base
> ./a.out
结果是:
Aborted
使用 GNU 调试器进行了一些操作后发现,catch 子句永远不会到达。不过,它似乎可以工作。
为什么我没有进入 catch 子句?
I have some sample code that doesn't run the way I think it should.
#import <Foundation/NSString.h>
#import <Foundation/NSException.h>
#import <Foundation/NSAutoreleasePool.h>
#import <stdio.h>
int main( int argc, const char *argv[] ) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
NSException *e = [NSException
exceptionWithName: @"NSException"
reason: @"The level is below 0"
userInfo: nil];
@throw e;
} @catch ( NSException *e ) {
printf( "+%s: ", [[e name] UTF8String] );
}
[pool release];
}
I run it as following:
> gcc -g -c main.m -fconstant-string-class=NSConstantString -I/usr/include/GNUstep
> gcc main.o -lgnustep-base
> ./a.out
The result is:
Aborted
A little bit of playing with the GNU debugger showed me that the catch clause is never reached. It seems to work mac though.
Why do I not get into the catch clause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所需的答案可以在此邮件线程中找到:http:// www.mail-archive.com/[email protected]/msg11979.html
为了获得正确的 Objective-C 异常处理,人们可能会需要使用 -fobjc-exceptions 标志进行编译。
The answers needed can be found in this mail thread: http://www.mail-archive.com/[email protected]/msg11979.html
To get proper objective-c exception handling one might need to compile with the -fobjc-exceptions flag.