• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Should a Method set NSError* to nil when No Error?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Should a Method set NSError* to nil when No Error?


  • Subject: Re: Should a Method set NSError* to nil when No Error?
  • From: Jerry Krinock <email@hidden>
  • Date: Sat, 19 Apr 2008 13:10:37 -0700

On 2008 Apr, 19, at 11:24, Bill Bumgarner wrote:

If there is no error -- if the return value is set and valid -- then the behavior regarding the (NSError**) argument is undefined.

Agreed, although not the way I would have designed it.

Any caller relying upon the value to be...unchanged...is implemented incorrectly.


Thank you!  That's what I wanted some support for.

As such, you are certainly free to do something like:
...

    *anError = [NSError ...];  // Whoops
...


Except that the above line will cause a crash if the invoker was not interested in the details and passed anError = NULL.

Forgetting to test that anError != NULL before assigning it is a really easy mistake to make (oh, especially when coding in Mail, I know...but I've done it in actual work!) Also, it is a mistake that won't show up in testing unless the test suite covers the triggering error. Ouch.

So, as of 2 hours ago I'm using the following macro at the beginning of all my methods that take an NSError** argument:

#define SSYInitErrorP(_error_p) NSError* dummyError ; \
if (_error_p == NULL) { \
    _error_p = &dummyError ; \
} \
* _error_p = nil ;

I use it like this, with an (NSError**)error_p:

    SSYInitErrorP(error_p)

Besides setting *error_p to nil, which we agree is not necessary but I "just like", it also assigns error_p to a dummy NSError* if NULL was passed in, avoiding latent crashes.

As the caller of your own method, do not rely on *anError being set to nil. Doing so creates an impedance mismatch with the rest of Cocoa's behaviors and, thus, is simply asking for trouble.

I agree with that. Thanks again, Bill.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Should a Method set NSError* to nil when No Error?
      • From: Ali Ozer <email@hidden>
References: 
 >Should a Method set NSError* to nil when No Error? (From: Jerry Krinock <email@hidden>)
 >Re: Should a Method set NSError* to nil when No Error? (From: Bill Bumgarner <email@hidden>)

  • Prev by Date: presentError:
  • Next by Date: Re: presentError:
  • Previous by thread: Re: Should a Method set NSError* to nil when No Error?
  • Next by thread: Re: Should a Method set NSError* to nil when No Error?
  • Index(es):
    • Date
    • Thread