Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
- Subject: Re: Why Don't Cocoa's (Un)Archiving Methods return Errors?
- From: Rainer Brockerhoff <email@hidden>
- Date: Fri, 29 Jul 2011 21:02:51 -0300
On 29/07/2011, at 20:47, email@hidden wrote:
> From: Jens Alfke <email@hidden>
> Date: 29 de julho de 2011 17:15:19 BRT
>
> On Jul 29, 2011, at 11:12 AM, Jerry Krinock wrote:
>
>> When invoking -archivedDataWithRootObject: on, say, dictionary, finding an un-encodeable object buried somewhere in the dictionary would seem to be quite common.
>
> This is a case of passing invalid data, so throwing an exception is appropriate. It’s implicit that when you archive an object graph, all the objects in it must be archivable.
> ...
> Not to start another flamewar, but this is a great example of why Exceptions Are Good, since you don’t have to rip apart your API and add lots of extra parameters just to add error handling.
Looking back at code I wrote years ago, I see that I usually did things like
@try {
someObject = [NSKeyedUnarchiver unarchiveObjectWithData:someData];
} @catch (id exception) {
someObject = nil; // or some other reasonable default state
// mark someData as being corrupted to avoid its reuse
}
and this works well in my (admittedly not overly thorough) testing. I do the same when reading in nib files, image resources, etc..
However, I've also seen cautions that after throwing _any_ exception whatsoever we should terminate the application as soon as possible.
Granted that NSKeyedUnarchiver might have left some memory leaks or partially uninitialized objects somewhere, but unarchiving an invalid object should happen rarely if at all... and ordinarily my own code should never get pointers to such objects, anyway.
While personally I never had any problems with that try/catch block, I'd be interested in other opinions and experiences.
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
Weblog: http://www.brockerhoff.net/blog
_______________________________________________
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