Is there a way to catch or handle EXC_BAD_ACCESS?
Nope; EXC_BAD_ACCESS
means things have gone wildly off the rails. Your program is trying to access a memory address that is invalid. I.e. memory has been corrupted and there is no predictable recovery.
It may be a memory management issue. If you can reproduce the issue, turn on NSZombies and see what happens. Or post the backtrace here.
Note that try-catch style exceptions are non-recoverable in iOS/Cocoa, too. Exceptions are not to be used for recoverable error handling. That is what NSError is for.
You can sometimes catch it in main
, with a signal handler. Doesn't allow you to do much, though, other than maybe log some stuff.
A try catch can be used but you'll first need to know what caused the issue. You can enable NSZombie for your current build to catch the error and eliminate the need. Edit current scheme, enable NSZombie.
- Update * Swift2+ has excellent error handling now and is definitely worth checking out. Swift Error Handling
A new C library SignalRecovery can enable programs to recover from operating system exceptions such as EXC_BAD_ACCESS
.
It can be used in IOS/MacOS/Linux
.
Sample code:
signal_try(label) {
// Add your code need try.
int* ptr = NULL;
*ptr = 0;
}
signal_catch(label) {
// Add your code to process exceptions, or do nothing.
siginfo_t* info = signal_info();
}
signal_end(label)
// Continue run