Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try-Catch equivalent in Objective-C

Tags:

objective-c

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?

like image 383
IIS7 Rewrite Avatar asked Dec 08 '12 04:12

IIS7 Rewrite


3 Answers

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.

like image 186
Sergey Kalinichenko Avatar answered Dec 10 '22 16:12

Sergey Kalinichenko


enter image description hereenter 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");
 }
like image 21
Srikanth Avatar answered Dec 10 '22 15:12

Srikanth


    //////////////////////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
}
like image 35
Alok Singh Avatar answered Dec 10 '22 17:12

Alok Singh