如何捕获BlockCode中的异常(目标C)

Zil*_*lan 6 objective-c objective-c-blocks

是否有正确的方法来捕获块代码中的异常?

我得到以下代码:

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
   }
}
Run Code Online (Sandbox Code Playgroud)

回调方法从另一个线程调用.如果响应等于400,则该DoSomethingWithRespond方法将抛出异常.

Kru*_*nal 4

    @try {
        <#statements#>
    }
    @catch (NSException *exception) {
        <#handler#>
    }
    @finally {
        <#statements#>
    }
Run Code Online (Sandbox Code Playgroud)