In Objective-C throwing
NSException is a bad way to control flow of the program. Usually we want to return a boolean result and set
NSError to indicate there was an error.
Sometimes we need to write code that catches exceptions thrown from a framework or third party library we have no control.
Take for example
Apple Documentation for seekToFileOffset has this warning:
So the code that uses seek to file offset has to be wrapped into
@try block. And if we want to report the error we have to have a
Here is an example of code that uses
Here is a test that verifies the behavior when an exception is caught:
The challenge becomes running those tests in debugger. If you have Xcode Exception Breakpoint set in debugger then debugger going to stop on that test every time. If you do not have exceptions enabled then debugger won’t catch other unexpected exceptions during test run.
I used to run tests two times once with exceptions disabled and if there is a failure in a test second time with enabled exceptions to debug the problem. One the second run I had to tell debugger to continue every time it stopped on expected exceptions until I get to the test failure. It would be nice if we can tell debugger to skip expected exceptions.
Here is a debugger command you can add to the Exception Breakpoint that will continue execution when special global variable
debuggerContinueOnExceptions is set to TRUE:
SpecHelper.h defines it as
and sets it to NO within
Now any test that expects the exception can set it to YES before the test and restore it to NO after.
With this change I can always have exceptions breakpoint enabled and run tests in debugger. When an unexpected is exception happens debugger stops and let me investigate the problem.
Complete example project with shared exception breakpoint can be found here: https://github.com/paulz/XcodeDebuggerContinueOnExceptions