捕获 dispatch_once 块中生成的异常

rvi*_*007 7 exception-handling ios

在处理 Apple 的按需资源框架时,我的应用程序发现为一部分用户生成了许多异常。我解决这个问题的第一次尝试是将我的代码包装在 @try/@catch 块中,但不幸的是,崩溃仍然传播。我已经缩小了问题的范围,但不确定如何解决它。似乎 Apple 使用 NSBundleResourceRequest 的代码使用了 dispatch_once 块,在其中生成了 Obj-C 异常。我编写了以下测试代码来查看使用 dispatch_once 块进行异常处理的行为:

 static dispatch_once_t onceToken;
 @try {

    // 1.) Expected: Caught by first catch block
    // @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't find" userInfo:nil];

    // 2.) Expected: Caught by second catch block
    // @throw NSInternalInconsistencyException;

    // 3.) Will not be caught be the surrounding @try/@catch block
    dispatch_once(&onceToken, ^{
        @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't find" userInfo:nil];
    });
} @catch(NSException* e) {
    DDLogError(@"Catch: NSException");
} @catch(id e) {
    DDLogError(@"Catch: Obj-C");
} @catch (...) {
    DDLogError(@"Catch: ...");
}
Run Code Online (Sandbox Code Playgroud)

正如我的评论中所见,dispatch_once 块中生成的异常不会被周围的@try/@catch 捕获。

我的问题是:

如何捕获我无权访问其源代码(Apple 的)的 dispatch_once 块中生成的异常?

除了不使用 Apple 的 ODR 框架之外,我还能做些什么来使我的应用程序在此问题上更加稳定?

附加信息(如果有帮助):

我试图解决的具体堆栈跟踪是以下代码。这不是 NSBundleResourceRequest 的唯一问题,但所有其他问题都遵循相同的范例。

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSBundleResourceRequest could not connect to its helper daemon'

Last Exception Backtrace:
0   CoreFoundation                       0x00000001857f3d38 __exceptionPreprocess + 124
1   libobjc.A.dylib                      0x0000000184d08528 objc_exception_throw + 52
2   Foundation                           0x000000018623e9d0 _setupConnection + 1688
3   libdispatch.dylib                    0x0000000185179048 _dispatch_client_callout + 12
4   libdispatch.dylib                    0x000000018517c710 dispatch_once_f$VARIANT$mp + 56
5   Foundation                           0x000000018623e2e8 +[NSBundleResourceRequest _connection] + 64
6   Foundation                           0x000000018623d1ac -[_NSBundleODRDataForApplications initWithBundle:] + 124
7   Foundation                           0x000000018623d07c +[_NSBundleODRDataForApplications dataForBundle:createIfRequired:] + 180
8   Foundation                           0x000000018623fba0 -[NSBundleResourceRequest conditionallyBeginAccessingResourcesWithCompletionHandler:] + 76
Run Code Online (Sandbox Code Playgroud)

小智 0

因为块在 GCD 调用中运行

\n
void _dispatch_client_callout(void *ctxt, dispatch_function_t f)\n{\n    @try {\n        return f(ctxt);\n    }\n    @catch (...) {\n        objc_terminate();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

捕获 NSException\xe3\x80\x82

\n