Alex Rivera | Logout

Try-Catch equivalent in Objective-C

Asked 2012-12-08T04:52:49.383
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?

Edit
Report

1 Answer

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");
 }
answered 2012-12-08T04:59:21.770

Your Answer