@try/@catch doesn't catch exception

2019-02-11 00:49发布

问题:

After upgrading Xcode from 6.0 to 6.3, I noticed my automated tests started failing.

2015-04-14 18:56:50.625 xctest[48304:517820] *** Terminating app due to uncaught exception 'foo', reason: 'bar'
*** First throw call stack:
(
    0   CoreFoundation                      0x0084a746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x004d3a97 objc_exception_throw + 44
    2   CoreFoundation                      0x0084a66d +[NSException raise:format:] + 141
    3   TestTests                           0x03e6dbdc -[TestTest test] + 810
    4   CoreFoundation                      0x0072b84d __invoking___ + 29
    5   CoreFoundation                      0x0072b6f8 -[NSInvocation invoke] + 360
    6   XCTest                              0x20108975 -[XCTestCase invokeTest] + 320
    7   XCTest                              0x20108baa -[XCTestCase performTest:] + 175
    8   XCTest                              0x20114122 -[XCTest run] + 314
    9   XCTest                              0x20107598 -[XCTestSuite performTest:] + 406
    10  XCTest                              0x20114122 -[XCTest run] + 314
    11  XCTest                              0x20107598 -[XCTestSuite performTest:] + 406
    12  XCTest                              0x20114122 -[XCTest run] + 314
    13  XCTest                              0x20107598 -[XCTestSuite performTest:] + 406
    14  XCTest                              0x20114122 -[XCTest run] + 314
    15  XCTest                              0x20103df2 __25-[XCTestDriver _runSuite]_block_invoke + 61
    16  XCTest                              0x20110c42 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 184
    17  XCTest                              0x20103d16 -[XCTestDriver _runSuite] + 285
    18  XCTest                              0x20104961 -[XCTestDriver _checkForTestManager] + 272
    19  XCTest                              0x20104c79 -[XCTestDriver runTestSuite:completionHandler:] + 376
    20  XCTest                              0x2011772c +[XCTestProbe runTests:] + 216
    21  libobjc.A.dylib                     0x004e967e +[NSObject performSelector:withObject:] + 70
    22  xctest                              0x0000823a xctest + 4666
    23  xctest                              0x000084b0 xctest + 5296
    24  xctest                              0x00008591 xctest + 5521
    25  xctest                              0x00007e3d xctest + 3645
    26  libdyld.dylib                       0x019d4ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

After further investigation it appears exceptions are now uncatchable. Both these snippets would cause this error.

XCTAssertThrows([NSException raise:@"foo" format:@"bar"]);

and

@try {
    [NSException raise:@"foo" format:@"bar"];
}
@catch (NSException *ex) {
    NSLog(@"caught");
}

After a VCS force clean sync and rebuild everything looked fine, but then the same failure started in a couple of builds time. Does anyone have any ideas?

回答1:

Following the developer forums, this appears to be a bug or oversight in the developer tools that introduced an incompatibility with binaries built with older versions of the tools.

Should be fixed in Xcode 6.4