views:

38

answers:

1

Hey hey!

I'm making an iphone based app and I have issues catching exceptions. So far, I've never had problem with try catches but here... well :D

Here is the code that doesn't catch any exception :

 - (void)updateView:(NSTimer*)t {

    NSMutableDictionary *requestResult = [[[NSMutableDictionary alloc] init] autorelease];

    @try {
        requestResult = [self.eqParam getParameters];
    }
    @catch (MMConnectionFailed * e) {
        [self performSelectorOnMainThread:@selector(noConnection) withObject:@"Could not reach server." waitUntilDone:YES];
    }
}

The lower side methods throw well exceptions in debug mode in case of an exception but when this method is concerned, nothing is caught.

Any clue?


UPDATE :

Finally, I found out where the problem was but I still don't know why the exception wasn't thrown on the lower lever. I changed the end of my getParameters method. Here :

- (NSMutableDictionary *)getParameters {

    @try {
        // be careful with NSMutableDictionary. Has to be used with setters to be correctly affected
        lastResponse = [MMSoapMethods getEquipmentParametersWithUserString:user equipmentId:equipmentId];
    }
    @catch (MMConnectionFailed * e) {
        @throw e;
    }
    @finally {
        if (self.lastResponse) {
            return lastResponse;
        }       
        else
            return nil;
    }
}

I just removed the @finally surrounding tags and the exception was thrown. Strange, isn't it?

+1  A: 

I think @finally trumps anything else. Basically, never ever return a value from an @finally block.

Refactor your code for getPArameters like this:

- (NSMutableDictionary *)parameters // Objective-C naming convention - no get
{

        // be careful with NSMutableDictionary. Has to be used with setters to be correctly affected
        // your version did not retain the return result.  This does, as long as the property lastResponse is retain
        self.lastResponse = [MMSoapMethods getEquipmentParametersWithUserString:user equipmentId:equipmentId];
        return self.lastResponse;

        // no need to catch an exception just so you can throw it again
}

I think the above is equivalent to what you had, except it doesn't returna value from the finally block and lastReponse won't disappear from under you (assuming you are using ref counting rather than GC).

JeremyP
Yeah you're right, my code was a little dumb... Thank you very much for your answer, it's perfect!
Romain