1

I am developing an iPhone app using Core Data and ahev noticed that I get exceptions when I would expect and error, this is an example:

NSError *error;

// exception here if edited attribute name todoText in modeller and generated new database with that new name in it(ie clearing the iphone), ie tring to access a field not in the database

@try {

   NSMutableArray *mutableFetchResults = [[todoListManagedObjectContext   executeFetchRequest:request error:&error] mutableCopy];

   //seems like vars declaered inside a try is only known inside it, so process here

   if (mutableFetchResults == nil) { // nil = error

      // Handle the error.

As seen in the comments I don't get an error for trying to access a non existing field....why not just use the error return system for this?

I guess my question is, what are errors and exceptions for, and do I really need to test for both like this?

Rgds PM

gerry3
  • 21,420
  • 9
  • 66
  • 74
Petter Magnusson
  • 309
  • 1
  • 4
  • 16

1 Answers1

3

What exception are you getting? Throughout Cocoa, exceptions generally mean programmer error so it suggests you're passing a parameter which is not valid. Trying to access a field which isn't in the database seems like such a situation; it's not a "something went wrong" error, it's a "you did something wrong" error.

  • Thanks Graham, thanks for that info! So, what could generate an error here, and is that at all likely on the iPhone? Oe what would have actually happened? – Petter Magnusson Aug 03 '09 at 22:48
  • So there is rarely any need to test for exceptions, other than for debugging purposes during development? – Petter Magnusson Aug 03 '09 at 22:50
  • Essentially the trick is to be ready to handle errors, because they could go wrong _in the course of your app_. Exceptions should be weeded out at development time as meaning you're doing the wrong thing with regard to the API. No Cocoa API throws to indicate a reasonable error condition. –  Aug 03 '09 at 22:51
  • Ah, exactly the info I needed, thank you! (cant vote for your answer yet...) – Petter Magnusson Aug 04 '09 at 05:34