简体   繁体   中英

can we prevent crash by using @try, catch mechanism .if the error is -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

I am new to iPhone,

Our Team created an App and uploaded to "AppStore", We integrated "Bug Sense" also.

Now our app got negative reviews because of crash, We are tested our App in iPhone/iPad 6.1.3

In my bug sense we got this report like below:

1st Error:

-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array [open]

2nd Error:

-[__NSArrayM objectAtIndex:]: index 61 beyond bounds for empty array [open]

Now IN my project i placed @try{ } catch blocks to the methods which uses " objectAtIndex " to prevent the crash,

and i placed the condition also ie, if the array count is grater than "0" then only it will enter in to my condition which uses " objectAtIndex "

My question is, can we prevent the above errors crash by using @try, catch mechanism .

Thanks in Advance

No, you should not use @catch to recover from exceptions and continue execution as if nothing happened .

For two reasons:

First, you have a bug in your code . Using @catch to catch the exception and ignore it is not a fix. You are simply addressing a symptom, but the bug remains.

Secondly, exceptions for flow control -- exceptions for handling recoverable errors -- is explicitly not supported in iOS/Cocoa programming. If an exception is thrown through a call to the system, the behavior is undefined.

If you would like more details, see my answer here: Usage of NSException in iPhone Apps

In the Apple's Documentation , you have exactly this example:

[...] the case of an NSArray, for example, you should always check the array's count to determine the number of items before trying to access an object at a given index. The objectAtIndex: method throws an exception if you make an out-of-bounds request so that you can find the bug in your code early in the development cycle—you should avoid throwing exceptions in an app that you ship to users.

This wont break

if([array count]>n)
obj =[array objectAtIndex:n]

You may extend NSArray with a category providing one (or a number of similar) methods.

- (id) saveObjectAtIndex:(NSUInteger)n {
    return (n < [self count]) ? [self objectAtIndex:n] : nil;
}

Or use a macro for that, which may be the better approach in terms of performance.

Frankly, I never did that myself. Why? If I did that then I had to check for nil and react on that accordingly. So in the end it is the same amount of work as checking for positive values (if the index variable is signed) and compare it with the count property and then react on the erroreous condition. Psycologically, doing it myself every time guides me writing quality code and supports my awareness for error tolerant coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM