2011-07-27 8 views
0

私はtryブロックにコードブロックを配置したプロジェクトを開発しています。例外が発生した正確なポイントを取得するには?

このブロックには、imageViewに画像を割り当てるための5つのステートメントが含まれています(画像フォルダに画像を追加する5つの画像ビューがあります)。リソースフォルダにイメージがない場合は、catchブロックにジャンプします。

私は画像にnilを割り当てることができるように、例外が発生している5つのうち正確なステートメントを知りたかったのです。

@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; 
} 
+0

どのような例外が発生しますか?答えはそれに依存します。 – Yuji

答えて

2

通常、プログラムフローには例外を使用しないでください。代わりに、bookAtIndex:メソッドを変更して、インデックスが範囲外になったときにnilを返すか、さらに適切には、それぞれを取得する前に書籍の数を確認するだけです。

例外と例外処理の詳細については、my answer to this questionを参照してください。

関連する問題