Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:27:55+00:00 2026-05-24T04:27:55+00:00

I’m new to Objective-C, and I see that there are different conventions used about

  • 0

I’m new to Objective-C, and I see that there are different conventions used about error handling. There are exceptions, but also there are situations where functions are just supposed to return nil in case of something going wrong.

So, how do I decide when use which, and how to handle exceptions and unexpected return values? What are the best practices and red flags?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:27:56+00:00Added an answer on May 24, 2026 at 4:27 am

    I won’t be definitive about which to use, but here’s some info about each of the options:

    Exceptions

    Exceptions in Obj-C are not really meant to be used to control program flow. From the documentation on exception handling:

    The general pattern is that exceptions are reserved for programmer error only, and the program catching such an exception should quit soon afterwards.

    For this reason I wouldn’t recommend using exceptions @try/@catch just to test whether a method worked correctly.

    You also have several options for handling exceptions, in addition to setting a higher-level uncaught exception handler.

    Errors

    Errors are typically used in three ways:

    Delegate methods

    An object can simply pass an NSError to its delegate in a designated error-handling callback:

    - (void)myObject:(MyObject *)obj didFailWithError:(NSError *)error;
    

    The delegate is then free to take any appropriate action, including perhaps displaying a message to the user. This pattern is commonly used in asynchronous delegate-based APIs.

    Out parameters

    These are most commonly used in conjunction with a boolean return value: if the return value is NO, then the NSError object can be examined for more information about the error.

    - (BOOL)performTaskWithParameter:(id)param returningError:(out NSError **)error;
    

    Where one possible usage pattern would be:

    NSError *error;
    if (![myObject performTaskWithParameter:@"param" returningError:&error]) {
        NSLog(@"Task failed with error: %@", error);
    }
    

    (Some people also prefer to store the boolean result in a variable before checking it, such as BOOL success = [myObject perform...];.) Due to the linear nature of this pattern, it’s best used for synchronous tasks.

    Block-based completion handlers

    A fairly recent pattern since the introduction of blocks, yet a quite useful one:

    - (void)performAsynchronousTaskWithCompletionHandler:(void (^)(BOOL success, NSError *error))handler;
    

    Used like this:

    [myObject performAsynchronousTaskWithCompletionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            // ...
        }
    }];
    

    This varies a lot: sometimes you won’t see the boolean parameter, just the error; sometimes the handler block has no arguments passed to it and you just check a state property of the object (for example, this is how AVAssetExportSession works). This pattern is also great for asynchronous tasks, when you want a block-based approach.

    Handling errors

    Cocoa on Mac OS X has a quite thorough error-handling path. There is also NSAlert’s convenience method + (NSAlert *)alertWithError:(NSError *)error;. On iOS, the NSError class still exists, but there aren’t the same convenience methods to handle errors. You may have to do a lot of it yourself.

    Read the Error Handling Programming Guide for more information.

    Returning nil

    This is often used in conjunction with NSError out parameters; for example, NSData’s method

    + (id)dataWithContentsOfFile:(NSString *)path
                         options:(NSDataReadingOptions)mask
                           error:(NSError **)errorPtr;
    

    If reading the file fails, this method returns nil, and further information is stored in an error.

    One reason this is a particularly convenient pattern is because of nil messaging, which can be done safely with no effect in Obj-C. I won’t go into detail here on why this is useful, but you can read more about it elsewhere on the interwebs. (Just make sure to find an up-to-date article; it used to be that methods returning floating-point values wouldn’t necessarily return 0 when sent to nil, but now they do, as described in the documentation.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.