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 8333285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:58:35+00:00 2026-06-09T02:58:35+00:00

I come from a Java background, where try/catch is common practice. After few months

  • 0

I come from a Java background, where try/catch is common practice.

After few months working with objective-c I have never implemented try/catch in my iOS app, because of the nature of the language itself, and general advices for not using it.

Therefore I have always tried to build error free code. But, at time of writing, I still didn’t know how a simple thing like that can be translated in objective-c:

// some operation
try {
  // do some work
} catch (SomethingWentWrongException e) {
  // log error, inform user...etc
}
// some more operation and rest of the code

With this code operation around try/catch is executed, but the program is not broke, and the rest of the code after catch is executed.

However there’s a rule telling that we shouldn’t use try catch for flow control.
I would like to implement something similar with objective-c, encapsulate some less important operations that may succeed or not, without breaking the workflow.

How can I do that without objective-c @try ?

thanks

  • 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-06-09T02:58:37+00:00Added an answer on June 9, 2026 at 2:58 am

    You can do that with Objective-C using @try and @catch (and @finally). There’s no “rule” that says you don’t, but there is a pervasive design idiom throughout Cocoa and code build to integrate with Cocoa: exceptions are reserved for programmer error. That means if the person using your API doesn’t honour the contract, or doesn’t satisfy some preconditions for calling your methods, that is the only time that many Objective-C developers would use exceptions.

    The common idiom for handling errors (e.g. my file went away, I couldn’t get the resource I needed etc) in Objective-C code is to use Cocoa’s NSError class, passing an instance by reference to the method that could possibly fail. So, rewriting your example:

    // some operation
    NSError *error;
    BOOL itWorked = [object doSomeThingThatCouldFail: &error];
    if (!itWorked) {
        // log error, inform user etc.
    }
    // some more operation and rest of the code
    

    Notice that you check the return value of the method to check whether it succeeded or failed. The error object is guaranteed to be set on failure, but no promise is made about it on success.

    The way the implementation of the method works with the error:

    - (BOOL)doSomeThingThatCouldFail: (NSError *__autoreleasing*)error {
      //...
      if (!success && error) //check that the user actually supplied an error pointer
      {
        NSDictionary *errorDetails = @{ ... };
        *error = [NSError errorWithDomain: @"Your error domain" code: someInteger userInfo: errorDetails];
      }
      return success;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background and have been using C# for the last
I come from Java background, where we have data structures with interfaces that if
I come from a java background, but I'm working on a django-based website. To
I come from a Java background and I am learning Objective C. I am
I come from a Java background and recently decided to try creating a C++
I’m relatively new to working with Scala having recently come from a java background.
I have no experience in C++, and I come from a Java background. Lately,
I am using C# having come from a Java background - I have an
I am relatively new to working with Java and have come from a C/C++

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.