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

  • Home
  • SEARCH
  • 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 7938531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:50:14+00:00 2026-06-03T22:50:14+00:00

Okay so I’ve been working on a database class and was told to use

  • 0

Okay so I’ve been working on a database class and was told to use exception.
However I read on the Apple developer guide to avoid exceptions as much as possible and use NSError instead. (https://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Chapters/ocExceptionHandling.html)

So it got me thinking:
Am I using the right approach? Should I be using NSErrors in this case?

+(long)insertTrack:(Track *)track Database:(Database *)db {
@try {
sqlite3_exec(db.dataBase, "BEGIN", 0, 0, 0); //Start transaction

const char *sqlTrack = "INSERT INTO ...";
if(sqlite3_prepare_v2(db.dataBase, sqlTrack, -1, &addTrackStatement, NULL) != SQLITE_OK) {
     @throw [NSException exeption....]       
}

//Bind variables
sqlite3_bind_text(...);

if(SQLITE_DONE != sqlite3_step(addTrackStatement)) {
    @throw [NSException exeption....]
}
else {
    long insertedTrack = sqlite3_last_insert_rowid(db.dataBase);
    sqlite3_exec(db.dataBase, "COMMIT", 0, 0, 0); //End transaction

    return insertedTrack;
}
}
@catch(NSException *exception) {
    //Log the exception and ROLLBACK!
}
@finally {
    sqlite3_clear_bindings(addTrackStatement);
    sqlite3_reset(addTrackStatement);
    sqlite3_finalize(addTrackStatement); 
}
}

So again my question: Should I use a return value to indicate that something had gone wrong with either the preparing or executing of the sql statement? Should I use NSError or use Exception like I am right now?

  • 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-03T22:50:15+00:00Added an answer on June 3, 2026 at 10:50 pm

    The exception implementation on iOS is pretty crummy and inefficient. But since errors are (presumably) rare, and SQL operations are not speedy fast anyway, it probably doesn’t matter much (in terms of performance) which you use, so long as you don’t use exceptions for the “entry not found” and “no more data” cases.

    Exceptions can be handy where there are a lot of operations in sequence, since you don’t have to individually deal with error handling and clean up after each one. However, in the above case (where you still have to test for error after each op) the benefits are not that great.

    There is a “poor man’s exception” scheme that can be used in such cases:

    errorCode = NoError;
    
    do {
        rcode = operation1();
        if (rcode) { 
            errorCode = ErrorA;
            break;
        }        
        rcode = operation2();
        if (rcode) { 
            errorCode = ErrorB;
            break;
        }        
        rcode = operation3();
        if (rcode) { 
            errorCode = ErrorC;
            break;
        }
        <More stuff>
    } until(FALSE);
    
    if (errorCode == NoError) {
        <do normal path stuff>
    }
    else {
        <do error path stuff>
    }
    

    This is reasonably efficient on all platforms that I know of, and is readily translated into most other programming languages, even those lacking exceptions.

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

Sidebar

Related Questions

Okay, I am asked to prepare a university database and I am required to
Okay so this has been modified there is still 3 tables board, account, log
Okay, this one is pretty obvious to everyone who use Django and frequently asked
Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay, I've been messing around with the excellent JodaTime library, attempting to get a
Okay so here is my database: I have a page on my .net form
Okay so I have a simple HTML Input fragment: <input id=txtFirstName type=text class=txtBox runat=server
Okay i know i have to use the JDBC etc, but im not sure
Okay, so I've tried to use sort to vector of items so the size
Okay, first some background, I can't use any javascript library except YUI for this

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.