9

I get an app crash in main.m in my app and have no idea why the error is happening because xcode doesn't show me where the crash occurs, it shows me that it crashes at return UIApplicationMain(argc, argv ...) which tells me nothing.

Is there a way to have in Objective-C the equivalent of a try/catch in C++ to see exactly where the error is occuring?

IIS7 Rewrite
  • 779
  • 3
  • 16
  • 31
  • 2
    Check out the answer to [this question](http://stackoverflow.com/questions/8100054/no-exception-stack-trace-in-console-under-xcode-4-2-ios-5). – Hot Licks Dec 08 '12 at 05:07
  • Thank you so much. This solved my problem. The issue was the name of a selector method and an uppercase U and the actual method had a lowercase u. Compiler didn't catch that (bad, bad), but the solution in the link gave me exactly what the problem was. thanks again. – IIS7 Rewrite Dec 08 '12 at 05:18

3 Answers3

13

Although Objective-C does have an @try/@catch, it is not going to help you much. By the time you get to the @catch, the stack frame where the error happens would be gone.

What you need is to set up a breakpoint that breaks on exception: open the Breakpoint Navigator page (the second button from the right), and click [+] at the bottom of the navigator page. Choose "Add Exception Breakpoint...", then click [Done].

Now the program is going to break in the debugger each time that your program throws an exception.

Snowcrash
  • 80,579
  • 89
  • 266
  • 376
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Ok, I tried this. +1 for very good instructions. The problem is that it stops again at the same place in int main(int argc, char *argv[]). The stack trace is in assembly language, I have no idea how to read the exception. Any other details I'm overlooking? – IIS7 Rewrite Dec 08 '12 at 05:07
  • 2
    Add this to your `@catch`: `NSLog(@"Stack trace: %@", [exception callStackSymbols]);` – Hot Licks Dec 08 '12 at 05:09
  • @HotLicks Thanks for the comment and for a great link that you posted above! – Sergey Kalinichenko Dec 08 '12 at 05:21
8

enter image description here enter image description here

I gave the try catch syntax below, but in addition to that, what you can do is that, in Xcode, you can set breakpoints. In the left hand side on your project explorer, click on Breakpoints tab. At the bottom left, you will find a +. (pictures above) Click on that and it will give you an option to set exception breakpoint. What this will do is that it will stop at any line where there is a crash. You there fore need not set try catch statements every where.

@try {

    // Your statements here
 }
 @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
 }
 @finally {
    NSLog(@"finally");
 }
Srikanth
  • 1,725
  • 2
  • 10
  • 11
3
    //////////////////////ADVANCED TRY CATCH SYSTEM////////////////////////////////////////
    #ifndef UseTryCatch
    #define UseTryCatch 1
    #ifndef UsePTMName
    #define UsePTMName 0  //USE 0 TO DISABLE AND 1 TO ENABLE PRINTING OF METHOD NAMES WHERE EVER TRY CATCH IS USED
    #if UseTryCatch
    #if UsePTMName
    #define TCSTART @try{NSLog(@"\n%s\n",__PRETTY_FUNCTION__);
    #else
    #define TCSTART @try{
    #endif
    #define TCEND  }@catch(NSException *e){NSLog(@"\n\n\n\n\n\n\
    \n\n|EXCEPTION FOUND HERE...PLEASE DO NOT IGNORE\
    \n\n|FILE NAME         %s\
    \n\n|LINE NUMBER       %d\
    \n\n|METHOD NAME       %s\
    \n\n|EXCEPTION REASON  %@\
    \n\n\n\n\n\n\n",strrchr(__FILE__,'/'),__LINE__, __PRETTY_FUNCTION__,e);};
    #else
    #define TCSTART {
    #define TCEND   }
    #endif
    #endif
    #endif
    //////////////////////ADVANCED TRY CATCH SYSTEM////////////////////////////////////////






Use TRY CATCH IN ANY METHOD LIKE THIS


-(void)anyMethodThatCanGenerateException
{
    TCSTART


    TCEND
}
Alok Singh
  • 896
  • 6
  • 18