如何捕获 NSRangeException?
当在线服务器出现问题时,我希望我的应用程序能够正常运行。我尝试将危险线包装在 @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
语法错误吗?我尝试为 NSRangeException
添加一个 @catch
块,但似乎这不是正确的方法(它不是一个类)。
此外,服务器错误是由 [ratable getRemoteId]
有时返回 (null)
而不是整数引起的。这种行为看起来相当不可预测;如果有人知道为什么 ObjectiveResource
可能会这样做,那将会很有帮助。但我仍然想知道如何使用@try @catch
。
I want my app to continue gracefully when the online server messes up. I tried to wrap the dangerous line in a @try
block. Yet it is still crashing like so:
the method:
+ (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;
}
the stack trace:
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'
Is my syntax for @try @catch
wrong? I attempted to add a @catch
block for NSRangeException
but it seems that's not the right approach (it's not a class).
Also, the server error is caused by [ratable getRemoteId]
sometimes returning (null)
instead of an integer. This behavior seems pretty unpredictable; if anyone has a clue why ObjectiveResource
might be doing that it would be helpful. But I still would like to know how to use @try @catch
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我现在的理解,抛出异常应该只是为了提醒库的用户他们犯了编程错误。我仍然好奇为什么我使用的语法不能防止崩溃。我知道错误发生在几个级别以下;但是 @try {} @catch {} 块应该处理我调用的方法调用的所有方法...
无论如何,对于任何想要从 Rails 风格的静态资源获取作用域对象的人来说,这里是固定的代码。
As I now understand it, throwing exceptions should only be done to alert users of your library that they have made a programming error. I am still curious why the syntax I used did not prevent the crash. I know the error was occurring several levels down; but the @try {} @catch {} block should handle all methods called by the methods I call...
At any rate, here is the fixed code, for anyone who wants to fetch scoped objects from a Rails-style restful resource.
你不知道。您修复代码以在这些情况下不引发异常。
You don't. You fix your code to not throw an exception under these circumstances.