Re: exception in init
Re: exception in init
- Subject: Re: exception in init
- From: Jean-Daniel Dupas <email@hidden>
- Date: Wed, 04 Apr 2012 18:18:04 +0200
Le 4 avr. 2012 à 17:57, Jens Alfke a écrit :
>
> On Apr 4, 2012, at 8:37 AM, Andreas Grosam wrote:
>
>> In a "debug" version, use NSAssert and friends (ensure Preprocessor macro "NS_BLOCK_ASSERTIONS" is not defined). Use Unit tests in order to detect *any* possible logic error. In a "release" version where NSAsserts and friends may become no ops, this may possibly ensure that no invalid parameters will be ever passed to the method.
>
> I don't think NS_BLOCK_ASSERTIONS is normally set for you in a Release build. Of course you can change the project settings to do so, but I don't know how common that is — Apple generally ships its apps and frameworks with assertions enabled, and I've never turned them off in anything I've written.
>
>> However, if you cannot guarantee that - or if you find this assumption too hairy - the best you probably can do is:
>>
>> if (url == nil) {
>> NSLog(@"FATAL ERROR: invalid parameter in initWithURL:");
>> abort();
>> }
>
> I disagree.
> First, if the developer is turning off assertions, it's because s/he doesn't want the overhead (in code size and runtime) of these kinds of checks. So except in very unusual circumstances you should honor that and not force your check to happen anyway.
> Second, calling abort() is way too harsh. Call +[NSException raise:] instead.
>
> —Jens
While +[NSException raise:] is a convenient method, it is not annotated as 'noreturn' and so don't let the compiler/analyzer know about the control flow.
Using @throw does not have such issue and may prevent unexpected warnings (for example about uninitialized variable or about missing return statement).
-- Jean-Daniel
_______________________________________________
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