Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exceptions within BlockCode (Objective C)

Is there a proper way to catch exceptions within block code?

I got the following code:

void(^callback(int) = ^(int respond){
   [self DoSomethingWithRespond:respond]; //this throws an exception
};

-(void)DoSomethingWithRespond:(int)respond{
   if(respond == 400){
     NSException *exception = [NSException 
                              exceptionWithName:@"Failed" 
                              reason:logMessage 
                              userInfo:nil];
     @throw exception
   }
}

The callback methods gets called from another thread. If the respond is equal to 400 the DoSomethingWithRespond method will throw an exception.

like image 517
Zillan Avatar asked Jun 04 '12 07:06

Zillan


1 Answers

    @try {
        <#statements#>
    }
    @catch (NSException *exception) {
        <#handler#>
    }
    @finally {
        <#statements#>
    }
like image 169
Krunal Avatar answered Oct 12 '22 22:10

Krunal