Re: @try @catch
Re: @try @catch
- Subject: Re: @try @catch
- From: Jean-Daniel Dupas <email@hidden>
- Date: Thu, 14 Aug 2008 14:37:17 +0200
Le 14 août 08 à 13:53, Devon Ferns a écrit :
On 14-Aug-08, at 7:21 AM, Graham Cox wrote:
On 14 Aug 2008, at 8:58 pm, Georg Seifert wrote:
is it recommended to use @try .. @catch blocks as flow control
like it is used in Python. They say explicitly to use it rather
than do a lot of test before just try if it works to look after it
only if it fails.
Hmmm, I'll be looking forward to the replies to this question!
My view is absolutely NOT to use try/catch for flow control. They
were invented to handle exceptional conditions, and that's what
they should be used for. They are not a substitute for proper
conditional clauses and properly structured code. I believe the
Python advice (which I take at face value, not being familiar with
Python) is misguided at best, though may suit that language better.
Also, my understanding is that while a try block is very cheap, the
catch block is not. Since they are only expected to be run once in
a blue moon, that's reasonable, but if they are used routinely,
performance is likely to suffer.
cheers, Graham
Many(probably all now) desktop CPUs are optimized for branch
prediction which would make if blocks more efficient. You can't
always say the same about embedded processors though.
My guess is you probably wouldn't notice much difference on a
desktop between the two without using some profiling to see the
actual speed difference. It'd likely be in the range of hundreds of
milliseconds.
I think it's still a horrible way of flow control. I think for some
reason Python must be more optimized for that method of flow control
but since Objective-C is based on C, it's more likely that the basic
if/else if will be more efficient. Message dispatch is not really
that much overhead.
I believe @try, @catch is faster than it used to be though.
@try / @catch, is still slow on 32 bits runtime and was optimize for
the 64 bits runtime to reduce the cost of @try, but it increase the
cost of @catch.
And as each exception have to create an NSException under the hood,
and to generate and record a stack trace for each exception, I'm not
sure it's efficient even on 64 bits runtime.
I agree with precedents answers which said you should avoid exceptions
as much as possible, and using them for control flow is really not a
good idea.
_______________________________________________
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