tags:

views:

119

answers:

2

Hi, 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]];
}
A: 

In Objective-C exceptions are different from errors. Try this:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:mainURL5]];
[request setHTTPMethod:@"POST"];

NSHTTPURLResponse* urlResponse = nil;  
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
if (error) {
    [clsMessageBox ShowMessageOK:@"ERROR" :[error description]];
} else {
    result5 = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
}

edit: also, unless you are doing this from a background thread, -[NSURLConnection sendSynchronousRequest:returningResponse:error:] is not recommended.

rpetrich
You shouldn't rely on checking error. It may not be nil even if an error occurred. The method returns nil on error, so if the method returns nil, only then should you look at what is in the error variable.
Jason Coco
That's not true, the method also returns `nil` in other situations (HTTP `204 No Content` for example). `error` will be set for OS/network failures. Applications should also check `returningResponse` to see what type of response the server has sent if that matters.
rpetrich
@rpetrich Actually, it is true. The state of error is undefined if the function succeeds, and it can be set (in fact, for CoreData it often /is/ set). This method NEVER returns nil on success. When you get a 204 response, the method returns an NSData object with a length of 0, not nil.
Jason Coco
+2  A: 

This method doesn't throw exceptions, so using try/catch is not going to help you. After the method returns, if responseData is nil, you have an error and you should check the value of error. You may also have an HTTP error, which will be part of the response data. Also, there is no reason to initialize error to some empty error object. Just initialize it to nil.

So:

NSError* error = nil;
NSData* responseData = nil;
if( !(responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]) ) {
  NSLog(@"Oops: %@", error);
}
Jason Coco