捕获NSInvalidArgumentException

时间:2014-05-09 07:14:29

标签: objective-c gcc gnustep

我最近在Linux上一直在使用Objective-C + GNUstep,但我无法理解为什么这段代码片段不能按照我认为应该的方式工作。将talk消息发送给pet导致NSInvalidArgumentException被抛出,就像我期望的那样,但由于某种原因它不会被catch块处理。是否有我遗漏的东西,或者这些异常应该是不可捕获的?

#import <Foundation/Foundation.h>
#import <Foundation/NSString.h>

@interface Dog : NSObject
{
}
-(void) speak;
@end

@implementation Dog
-(void) speak
{
    NSLog(@"arf!\n");
}
@end

int main(int argc, char **argv)
{
    Dog *pet = [Dog alloc];

    [pet speak];
    @try {
        [pet talk];
    }@catch (id error) {
        NSLog(@"Dogs can\'t do that, silly!");
    }

    [pet release];

    return 0;
}

编译器标志:

gcc -fgnu-runtime -fobjc-exceptions -fconstant-string-class=NSConstantString crash.m -lobjc -lgnustep-base -g

输出:

jay@localhost ~/programs/objc/why $ ./a.out
2014-05-08 17:07:05.169 a.out[20341] arf!
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x18ae4c0) of class GSCInlineString in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x18afd70) of class NSException in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x1851000) of class NSLongLongNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x1850dc0) of class NSLongLongNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x1850ab0) of class NSLongLongNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x184e7d0) of class NSLongLongNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x1850ca0) of class NSLongLongNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x184ec50) of class NSIntNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x184e620) of class NSLongLongNumber in thread <NSThread: 0x17a5ee0>
2014-05-08 17:07:05.170 a.out[20341] autorelease called without pool for object (0x1850c10) of class NSIntNumber in thread <NSThread: 0x17a5ee0>
./a.out: Uncaught exception NSInvalidArgumentException, reason: -[Dog talk]: unrecognized selector sent to instance 0x1818ef0

0 个答案:

没有答案