简体   繁体   中英

objective-c not catching exception in try/catch block

I have this statement inside a try/catch block, but the exception is never caught. In fact, any exception thrown is never caught. Can anyone help me?

Is there any setting disabled in the xcode project that disables catching exceptions?

NSString *test = @"test";
unichar a;
int index = 5;

@try {
    a = [test characterAtIndex:index];
}
@catch (NSException *exception) {
    NSLog(@"%@", exception.reason);
}
@finally {
    NSLog(@"Char at index %d cannot be found", index);
    NSLog(@"Max index is: %lu", [test length]-1);
}

* Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds' * First throw call stack: (0x1832ad900 0x18291bf80 0x1832ad848 0x1831a52f0 0x100249340 0x188015704 0x188244130 0x1882484b8 0x1882455c0 0x184863790 0x184863b10 0x183264efc 0x183264990 0x183262690 0x183191680 0x18800e580 0x188008d90 0x10017662c 0x182d328b8) libc++abi.dylib: terminating with uncaught exception of type NSException

In my case, there is a Other Liker Flag -Wl,-no_compact_unwind in the build setting of my project.

I remove it, then the problem be solved.

PIC

You can go to build settings -> Enable Objective-C Exceptions

It should solve the problem.

在此输入图像描述

Use the physical device instead of the simulator.

Notes here - https://gist.github.com/johndpope/89c00eccbdd6e87720724dabd82b8774

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM