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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With