5

我想编写一些代码来处理 HTTP 连接失败时的异常。我使用以下代码:

-(void) connection:(NSURLConnection *)connection
  didFailWithError: (NSError *)error {
    UIAlertView *errorAlert = [[UIAlertView alloc]
                    initWithTitle: [error localizedDescription]
                    message: [error localizedFailureReason]
                    delegate:nil
                    cancelButtonTitle:@"OK"
                    otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    [activityIndicator stopAnimating];
    NSLog (@"Connection Failed with Error");
}

但是当连接失败时,程序就会崩溃。如何让警报弹出而不会导致程序崩溃?

4

1 回答 1

3

您的代码没有明显错误,您需要提供更多信息。

确保你有一个断点objc_exception_throw,然后在调试器下运行程序。然后您可以确定在哪一行抛出异常。

一个疯狂的猜测,但也许[error localizedDescription][error localizedFailureReason]正在回归nil

于 2009-03-03T18:05:35.440 回答