0

When using CodeRunner to test Objective-C code snippets, any exception thrown during run time will cause a crash, followed by the <my program> quit unexpectedly alert with complete stack trace and crash report saved in ~/Library/Logs/DiagnosticReports.

The exception can for instance be the result of a misspelled method name, and can happen quite often, depending on your personal development style. It is worth noticing that this crash report is also sent to Apple, which can seem a bit excessive for a misspelled method name.

Can this alert and crash report be avoided?

Monolo
  • 18,205
  • 17
  • 69
  • 103

1 Answers1

1

The default code template can be changed for each programming language in the app's Preferences settings.

If a try-catch block is added, the snippet can catch all its own exceptions and for instance just print out a log statement, hence avoiding the crash report.

For Objective-C, it can look like this:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
    @try {

    %@

    } @catch (NSException *e) {
        NSLog(@"Exception caught: %@", e);
    }
    }
}

The %@ defines the initial insertion point when a new file is opened.

Monolo
  • 18,205
  • 17
  • 69
  • 103
  • That'll only work for thrown exceptions, not for hard crashes. – bbum Jun 29 '13 at 17:13
  • 1
    That's true, and thanks for pointing it out! For the code snippets that I typically compile and run in CodeRunner I only ever get thrown exceptions, though. Typically the misspelled method name mentioned above, for which a complete crash report sent to Apple also seems a bit excessive, not to mention the UX issue having to deal with the alert. Edited the title to reflect your comment. – Monolo Jun 29 '13 at 19:13