objective-c - 如何在 BlockCode 中捕获异常( Objective-C )

标签 objective-c objective-c-blocks

是否有适当的方法来捕获 block 代码中的异常?

我得到了以下代码:

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
   }
}

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

最佳答案

    @try {
        <#statements#>
    }
    @catch (NSException *exception) {
        <#handler#>
    }
    @finally {
        <#statements#>
    }

关于objective-c - 如何在 BlockCode 中捕获异常( Objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877848/

相关文章:

ios - dispatch_sync 在队列为空之前返回

ios - NSOperationQueue 在完成任务之前获得完成通知

objective-c - NSDecimalNumber 乘法奇怪

objective-c - 是否可以在另一个线程上运行 sqlite3_exec() ?

ios - 将 ObjC 转换为 Swift - UnsafePointer<Void> 不可转换为结构类型

ios - 单例是否在 block 内创建保留循环?

objective-c - 管理调用委托(delegate)回调 block 的对象的内存

ios - UITableViewController 的静态内容消失了

ios - 关闭 2 个 ViewController

iphone - 如何从完成 block 中检索返回值?