I developing an application in which i calling web services on iphone. I want to implement Try…….Catch in that code for catching internet and GPRS connection error.The code is as follow,
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:mainURL5]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[[NSError alloc] init] autorelease];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
result5 = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
I was using Try……catch but it didn’t work that code as follows,
@try
{
//prepar request
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:mainURL5]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[[NSError alloc] init] autorelease];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
result5 = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
}
@catch(NSException * e)
{
[clsMessageBox ShowMessageOK:@"some text" :[e reason]];
}
This method doesn’t throw exceptions, so using try/catch is not going to help you. After the method returns, if
responseDataisnil, you have an error and you should check the value oferror. You may also have an HTTP error, which will be part of the response data. Also, there is no reason to initializeerrorto some empty error object. Just initialize it tonil.So: