• 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: Exceptions vs. pointers to error objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exceptions vs. pointers to error objects


  • Subject: Re: Exceptions vs. pointers to error objects
  • From: "Ken Ferry" <email@hidden>
  • Date: Tue, 28 Oct 2008 04:02:32 -0700

> Perhaps there is already a policy on when each is to be used. If so, please
> enlighten me. :)

There is a policy in Cocoa.  I'm pretty sure it's documented, but I
didn't find it laid out just now..

Exceptions are for things that just shouldn't happen, or that are
essentially unrecoverable.  Throw an exception if the programmer using
your API made a mistake and passed parameters that it isn't in your
contract to handle.  People are not expected to be catching
exceptions.  NSApplication catches them up at the top of the event
loop, and will log.

NSError is for errors of the sort you'd want to tell the user about.
They're supposed to be directly presentable via -[NSApplication
presentError:] and friends.  If the thing that went wrong is internal
and you expect the programmer to handle it, NSError isn't appropriate.
 (A piece of calling code that has more user-centric information about
what went wrong than the one that generated the NSError may and should
replace the NSError with one that is more appropriate, though.)

For APIs that programmers may expect to fail and handle without
getting the user involved, Cocoa usually just returns a BOOL to say
"nope, didn't work", or returns nil or somesuch.  I think there are a
few APIs that that give a 'reason' why something happened.

-Ken

On Tue, Oct 28, 2008 at 12:58 AM, Colin Cornaby <email@hidden> wrote:
> I'm writing an API to communicate with a web service, and I was just
> wondering what the thinking is on exceptions vs. functions returning an
> NSError in some way. Basically I'm wondering what people's opinions are on a
> function throwing an exception on failure, vs returning an NSError object.
>
> Perhaps there is already a policy on when each is to be used. If so, please
> enlighten me. :)
>
> Thanks,
> Colin
> _______________________________________________
>
> 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
>
_______________________________________________

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

References: 
 >Exceptions vs. pointers to error objects (From: Colin Cornaby <email@hidden>)

  • Prev by Date: Re: Can we ask iPhone questions yet?
  • Next by Date: Re: alpha value from NSBitmapImageRep
  • Previous by thread: Re: Exceptions vs. pointers to error objects
  • Next by thread: Code signing validation
  • Index(es):
    • Date
    • Thread