我如何捕获NSRangeException?

时间:2010-08-07 20:53:59

标签: objective-c cocoa error-handling nsexception

当在线服务器搞砸时,我想让我的应用程序继续优雅地继续。我试图将危险的行包裹在@try块中。然而它仍然像这样崩溃:

方法:

+ (NSArray *)findAllFor:(NSObject *)ratable {
      NSString *ratingsPath = [NSString stringWithFormat:@"%@%@/%@/%@%@",
     [self getRemoteSite],
     [ratable getRemoteCollectionName],
     [ratable getRemoteId],
     [self getRemoteCollectionName],
     [self getRemoteProtocolExtension]];

     Response *res = [ORConnection get:ratingsPath withUser:[[self class] getRemoteUser] 
     andPassword:[[self class] getRemotePassword]];
     NSArray *ratings;
     @try {
          ratings = [self fromXMLData:res.body];
     }
     @catch (NSException *e) {
          ratings = [NSArray array];
     }
    return ratings;
}

堆栈跟踪:

Program received signal:  “SIGABRT”.
2010-08-07 16:38:51.846 TalkToHer[68608:7003] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** Call stack at first throw:  
(
 0   CoreFoundation                      0x02932919 __exceptionPreprocess + 185  
 1   libobjc.A.dylib                     0x02a805de objc_exception_throw + 47  
 2   CoreFoundation                      0x0292858c -[__NSArrayI objectAtIndex:] + 236  
 3   TalkToHer                           0x00009fa7 -[FromXMLElementDelegate parser:didEndElement:namespaceURI:qualifiedName:] + 425  
 4   Foundation                          0x0017bcc1 _endElementNs + 453  
 5   libxml2.2.dylib                     0x02d9deb6 xmlParseXMLDecl + 1353  
 6   libxml2.2.dylib                     0x02da8bc1 xmlParseChunk + 3985  
 7   Foundation                          0x0017b4c2 -[NSXMLParser parse] + 321  
 8   TalkToHer                           0x0000b14d +[NSObject(XMLSerializableSupport) fromXMLData:] + 201  
 9   TalkToHer                           0x00031a6c +[Rating findAllFor:] + 320  
 10  TalkToHer                           0x00032d67 -[FirstClassContentPiece(Ratable) updateRatings] + 96  
 11  TalkToHer                           0x00004d5f __-[InspirationController tableView:didSelectRowAtIndexPath:]_block_invoke_3 + 33  
 12  libSystem.B.dylib                   0x9792efe4 _dispatch_call_block_and_release + 16  
 13  libSystem.B.dylib                   0x97921a4c _dispatch_queue_drain + 249  
 14  libSystem.B.dylib                   0x979214a8 _dispatch_queue_invoke + 50  
 15  libSystem.B.dylib                   0x979212be _dispatch_worker_thread2 + 240  
 16  libSystem.B.dylib                   0x97920d41 _pthread_wqthread + 390  
 17  libSystem.B.dylib                   0x97920b86 start_wqthread + 30  
)
terminate called after throwing an instance of 'NSException'

@try @catch的语法错了吗?我试图为@catch添加NSRangeException块,但似乎这不是正确的方法(它不是一个类)。

此外,服务器错误是由[ratable getRemoteId]有时返回(null)而不是整数引起的。这种行为似乎很难以预测;如果有人知道为什么ObjectiveResource可能会这样做会有所帮助。但我仍然想知道如何使用@try @catch

2 个答案:

答案 0 :(得分:0)

你没有。您修复代码以在这些情况下不抛出异常。

答案 1 :(得分:0)

正如我现在所理解的那样,抛出异常只是为了提醒您的库用户他们发生了编程错误。我仍然很好奇为什么我使用的语法没有阻止崩溃。我知道错误发生了几个级别;但@try {} @catch {}块应该处理我调用的方法所调用的所有方法......

无论如何,对于想要从Rails风格的restful资源中获取范围对象的人来说,这是固定代码。

+ (NSArray *)findAllFor:(NSObject *)ratable {
    NSString *ratingsPath = [NSString stringWithFormat:@"%@%@/%@/%@%@",
                          [self getRemoteSite],
                          [ratable getRemoteCollectionName],
                          [ratable getRemoteId],
                          [self getRemoteCollectionName],
                          [self getRemoteProtocolExtension]];

    Response *res = [ORConnection get:ratingsPath withUser:[[self class] getRemoteUser] 
                      andPassword:[[self class] getRemotePassword]];
    NSError **aError;
    if([res isError]) {
        *aError = res.error;
        return nil;
    }
    else {
        return [self performSelector:[self getRemoteParseDataMethod] withObject:res.body];
    }
}
相关问题