My iPad app is using Objective-C for UI, and it calls some C++ functions for computation. Since the C++ code is not well written, it sometimes throw exceptions or causes segmentation fault when tested alone. However, the C++ code is currently under development by someone else so I don't want to change it. Is it possible to catch the exceptions and segmentation fault in my Objective-C code so that I don't need to change the C++ code? I tried the basic try-catch, but it seems not working. (wrapper is the buggy c++ function)
@try {
wrapper([imageName UTF8String]);
}
@catch (NSException *e) {
NSLog(@"Error");
}
When I run my app and click the button that calls C++ functions, the simulation crashes and an error message says libc++abi.dylib: terminating with uncaught exception of type NSException
you can use C++ try-catch
with Objective-C++ code (.mm extension)
try {
wrapper([imageName UTF8String]);
}
catch (...) {
NSLog(@"Error");
}
In 64-bit processes, you can use @catch(...) to catch everything including C++ exception
@try {
wrapper([imageName UTF8String]);
}
@catch (...) {
NSLog(@"Error");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With