Re: exception in init
Re: exception in init
- Subject: Re: exception in init
- From: Ariel Feinerman <email@hidden>
- Date: Thu, 05 Apr 2012 16:49:21 +0400
On Wed, Apr 4, 2012 at 9:38 PM, Dave <email@hidden> wrote:
> Hi,
>
> If it's a real error, this is what I do:
>
> - (id) initWithURL: (NSURL *) url
> {
> self = [super init];
> if (self == nil)
> return nil;
>
> if (url == nil)
> {
> [self release];
> return nil;
> }
>
> return self;
> }
>
> or in this case, you could just do this:
>
> I think in the following you make a memory leak
> - (id) initWithURL: (NSURL *) url
> {
> if (url == nil)
> return nil;
>
> self = [super init];
> if (self == nil)
> return nil;
>
> return self;
> }
>
> The caller should check for a nil value returned from the init method.
>
> If it's a programming error, e.g. it should never happen, then it's
> probably best to log the error and abort the App.
>
> Hope this helps.
>
> All the Best
> Dave
>
>
> On 4 Apr 2012, at 12:38, Ariel Feinerman wrote:
>
> Hi,
>>
>> I think the question was asked and answered but I cannot see a clearance
>> what is the right way to write init in the cases the arguments are nil or
>> wrong?
>> Returning a nil or raising an exception?
>>
>> - (id) initWithURL: (NSURL *) url {
>>
>>
>> if ((self = [super init])) {
>>
>> if (!url) {
>>
>> // ?
>>
>> }
>>
>> if (![url isFileURL]) {
>>
>> // ?
>>
>> }
>>
>> }
>>
>> return self;
>>
>> }
>>
>> I can frequently see the following in init
>>
>> NSAssert(url, @"url may not be nil", );
>>
>> --
>> best regards
>> Ariel
>>
>> ______________________________**_________________
>>
>> 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<http://lists.apple.com>
>>
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/**mailman/options/cocoa-dev/**
>>
>> This email sent to email@hidden
>>
>
>
--
best regards
Ariel
_______________________________________________
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