Question

I want to write some code to handle exceptions when HTTP connection fails. I use the following codes:

-(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");
}

But the programme just crashes when the connection fails. How to let the alert pop up without program crash?

Was it helpful?

Solution

Nothing is obviously wrong with your code, you will need to supply more information.

Make sure you have a breakpoint on objc_exception_throw and then run the program under the debugger. Then you can determine on what line the exception is thrown.

A wild guess, but perhaps [error localizedDescription] or [error localizedFailureReason] is returning nil.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top