0

I am developing a project in which I placed a block of code in a try block.

The block contains 5 statements in order to assign a image to a imageView (I have 5 imageviews to which I am adding images from resource folder). The flow jumps to catch block if there is no image in the resource folder.

I wanted to know the exact statement out of the five where the exception is getting raised so that I can assign the image a nil.

@try {
    if (indexPath.row%2==0) {
        cell.bookImageView1.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+0].titleImage;
        cell.bookImageView2.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+1].titleImage;
        cell.bookImageView3.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+2].titleImage;
        cell.bookImageView4.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+3].titleImage;
        cell.bookImageView5.image=nil;
    }else {
        cell.bookImageView1.image=nil;
        cell.bookImageView2.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+0].titleImage;
        cell.bookImageView3.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+1].titleImage;
        cell.bookImageView4.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+2].titleImage;
        cell.bookImageView5.image=[[KitabooBookListDataSource sharedDataSource] bookAtIndex:(indexPath.row*4)+3].titleImage;
    }
}


@catch (NSException *e) {
    NSLog(@"catch block");
    NSLog(@"array of exception %@",[e callStackReturnAddresses]);
    //NSMutableArray *catchArray=[[NSMutableArray alloc] initWithArray:[e callStackReturnAddresses]];
    //[[catchArray lastObject] ]=nil;
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
Dinakar
  • 1,198
  • 15
  • 37

1 Answers1

2

You generally shouldn't use exceptions for program flow. Instead, modify your bookAtIndex: method to return nil when the index is out of bounds... or even more appropriately, you should just check the number of books before trying to get each one.

Read my answer to this question for more about exceptions and exception handling.

Community
  • 1
  • 1
jtbandes
  • 115,675
  • 35
  • 233
  • 266