Re: Exceptions vs. pointers to error objects
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