Re: exception in init
Re: exception in init
- Subject: Re: exception in init
- From: Jens Alfke <email@hidden>
- Date: Wed, 04 Apr 2012 12:14:16 -0700
On Apr 4, 2012, at 11:48 AM, Klaus Backert wrote:
> Just to be sure what you mean: What is a real error? Simply an error which is not a programmer's (or programming?) error?
A runtime error, one that could occur in a valid program. What this means exactly is up to the designer of the API.
For example, let's say your init method takes a required file reference as an NSURL.
If this is nil, that's a programmer error —> exception.
If this is a URL of some other scheme than file:, that's probably also a programmer error —> exception.
If it's a file URL but the file doesn't exist or is unreadable, that cannot be a programmer error, simply because there is no way to preflight-check for that. —> return nil, ideally also an NSError as an 'out' parameter.
One of Cocoa's design principles is that a hypothetical 100%-debugged program should never raise any exceptions no matter what. That is, exceptions only result from programmer errors, not runtime issues.
(In practice, even Foundation doesn't adhere to this principle completely. Most notoriously, Distributed Objects will raise exceptions if a message-send to a remote object fails for legitimate reasons like a network problem. And since your code could be passed a proxy to a remote object without knowing it, that means pretty much anything could raise an exception. This is one of the reasons I avoid DO.)
For what it's worth, I really dislike and disagree with this design principle; I'd rather have a design that used exceptions to signal runtime error conditions — as in every other modern language — instead of kludges like "out NSError**" parameters that mess up everyone's class APIs. But it's much too late to do anything about this other than have lengthy pointless flame-wars, so never mind.
—Jens
_______________________________________________
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