views:

388

answers:

3

In Visual Basic, there's a line of code you can use to handle errors in code

"On Error Resume Next"

Is there something similar in Objective-C? My problem is I have an "Add New Cell" add button that creates a new cell during runtime. However, the cells are re-orderable. When you try to re-order the "Add New Cell" cell, I get an index out of bounds error. If I can just fix the error from occurring that would be great, but if there's an error handler I can use to just exit the move function when there is an error, that would be cool too.

Here is my moveRowAtIndexPath code:

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 iProfileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
 [appDelegate moveFromOriginal:fromIndexPath.row toNew:toIndexPath.row];
 }

Here is the error i receive:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (7) beyond bounds (7)'
+1  A: 

You should read this SO question about getting a stack trace for thrown exceptions. It will let you determine exactly where the exception is being raised so you can debug it. This one might also be helpful.

As an aside, you don't want to just ignore errors like this. The exception is being thrown for a reason. You should address that reason.

nall
+4  A: 

This is an array bounds error and should be fixed.

Have you tried preventing the user from moving the "Add New Cell" cell?

To do this, implement tableView:canMoveRowAtIndexPath: and return NO for the "Add New Cell" cell row (YES for all other rows).

gerry3
I have tried that, the problem is, cells are created dynamically, so there are additional indexes.
AWright4911
This is common. You should be keeping track of how many rows there are. Typically, the data rows are kept in an array and the number of rows is [dataArray count] + 1 where the 1 represents the number of non-data rows such as an "Add New Cell" row. In the tableView:canMoveRowAtIndexPath, you would check if the indexPath.row is [dataArray count] (index of the last row) and return NO for that row only (so it can't be moved).
gerry3
+2  A: 

You can use try and catch. They are how objective c, and some others, handle exceptions. They will catch the "uncaught exception" similar to how I assume would "On Error Resume Next" works.

@try {
    /*this is where the code that might throw an exception goes*/
    [appDelegate moveFromOriginal:fromIndexPath.row toNew:toIndexPath.row];
}
@catch (NSException *exception) {
    /*add something here if you want it to do something special when the exception(or "error") is thrown*/
}
@finally {
    /*you don't have to include this finally part but if you do the code in it is excecuted weather the exception is thrown or not*/
}
Jamvert
This worked perfect.Is there anyway to get the "Add New Cell" to return to the very last index if it is moved to a lower index? My goal is to have the "Add New Cell" always be at the very bottom of the Table. With the above code, it allows the "Add New Cell" row to moved anywhere in the table, and I dont want that.
AWright4911
You should not be catching the exception. You should be preventing the "Add New Cell" row from being moved in the first place. See my answer.
gerry3
Actually, this method works out fine. When the table is done editing, the "Add New Cell" goes back to the last index, and whatever new row is added is added at the end of the table. I may mess around with GERRY3's method, but this was just a bump in the road that was BUGging me!
AWright4911
Thats cool. I think Gerry3's answer is probably the more proper solution. Though, possibly, it is just a matter of preference. I am not familiar with UITableView it self.
Jamvert