3

I am trying to get Captions from a given instagram picture, however if there is no caption the app throws an exception and crashes. How would I implement @try and @catch to do this. Here is what i have so far:

@try {
    RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:[NSString stringWithFormat:@"%@",entry[@"user"][@"full_name"]] message:[NSString stringWithFormat:@"%@",text[@"caption"][@"text"]]];
    [modal show];
}
@catch (NSException *exception) {
    NSLog(@"Exception:%@",exception);
}
@finally {
  //Display Alternative
}
borrrden
  • 33,256
  • 8
  • 74
  • 109
Prad
  • 469
  • 1
  • 8
  • 28
  • Have you tried [exception reason] instead of just exception in your NSLog statement. – asafreedman Aug 07 '13 at 01:14
  • I really dont know how to could you tell me how? – Prad Aug 07 '13 at 01:19
  • 4
    I assume you are also throwing the exception? The use of exceptions for common situations like this is discouraged in Objective-C. Exceptions are for...well...exceptional situations that the program cannot recover from. Instead why not use an `NSError` object and signal the user if there is no caption. – borrrden Aug 07 '13 at 01:19
  • Try `NSLog(@"Exception %@", [exception reason]);` – asafreedman Aug 07 '13 at 01:19
  • @borrrden could you please show me an example of this i am kind of a novice – Prad Aug 07 '13 at 01:20
  • http://stackoverflow.com/questions/4654653/how-to-use-nserror-in-my-iphone-app Like this – borrrden Aug 07 '13 at 01:21
  • I tried doing it right now however it did not print anything nor did it stop the app from crashing @asafreedman – Prad Aug 07 '13 at 01:22
  • P.S. Is it throwing an exception or simply crashing (see this http://stackoverflow.com/questions/9745581/objective-c-try-catch-not-catching?rq=1 ) – borrrden Aug 07 '13 at 01:25
  • @borrrden could you show me how to implement the NSerror way with my code above im having trouble following the first link that you sent me. – Prad Aug 07 '13 at 01:31

1 Answers1

6

This is not a good use of exceptions and try-catch-finally blocks. You say that you get the exception if the caption is nil. So, what precisely do you want your app to do in order to gracefully handle this situation? Not show the dialog at all? Then you might do something like:

NSString *user = entry[@"user"][@"full_name"];
NSString *caption = text[@"caption"][@"text"];

if (caption != nil && caption != [NSNull null] && user != nil && user != [NSNull null]) {
    RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:user message:caption];
    [modal show];
}

Or perhaps you want to show something else if those are nil:

NSString *user = entry[@"user"][@"full_name"];
NSString *caption = text[@"caption"][@"text"];

if (caption == nil || caption == [NSNull null])
    caption = @"";     // or you might have @"(no caption)" ... whatever you want
if (user == nil || user == [NSNull null])
    user = @"";

RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:user message:caption];
[modal show];

Or, if you have the source code for RNBlurModalView, perhaps you can diagnose why precisely it's generating an exception when the caption is nil, and fix that issue there.

There are lots of possible approaches, dependent upon precisely what you want the app to do in these situation, but exception handling is undoubtedly not the right approach. As the Dealing With Errors section of the Programming with Objective-C guide, exceptions are intended for unanticipated "programmer errors", not simple logic errors, and as they say:

You should not use a try-catch block in place of standard programming checks for Objective-C methods.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • It didnt work here is what comes `libc++abi.dylib: terminate called throwing an exception` – Prad Aug 07 '13 at 03:53
  • `2013-08-06 23:52:32.878 Floadt[2589:c07] -[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x3102678 2013-08-06 23:52:32.880 Floadt[2589:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x3102678' libc++abi.dylib: terminate called throwing an exception` – Prad Aug 07 '13 at 03:54
  • @prnk28 Your error is telling you that the object is `[NSNull null]`. So just add checks for that, too. See revised answer. I'm assuming the error is coming from caption being `[NSNull null]`, so this edit should do it. If not, you have to identify precisely which object is getting set to `[NSNull null]`. – Rob Aug 07 '13 at 04:12
  • It still gives me the same error. – Prad Aug 07 '13 at 16:30
  • @prnk28 Looking at the error more closely, it's `objectForKeyedSubscript` (which is called when you use the `[]` syntax). So, either `entry` is `[NSNull null]` or `text` is `[NSNull null]` or `entry[@"user"]` is `[NSNull null]` or `text[@"caption"]` is `[NSNull null]`. You have to examine these values and figure out which one is giving you the `[NSNull null]` value. – Rob Aug 07 '13 at 16:37