Cost of Try ... Except

Hi
There have been a number of comments recently about Exceptions being very expensive in terms of performance.

I had assumed that Try Except would have a similar cost to Try Finally except in the case when an exception occurs and the exception parameters need to be marshaled.

It that a naive assumption and has anybody done tests or seen studies on the impact of exceptions.

I think the only time you really need to worry about it is where you are inside a tight loop - for the most part the overhead is much less than our own code.

So this will be slower

for i := 0 to list.count -1 do
begin
  try
   ...
  except
   ...
  end;
end;

Than this

try
  for i := 0 to list.count -1 do
  begin
  ....
  end
except
 ...
end

If you have enough items to iterate.

Disclaimer - not an expert… just my experience. If performance really matters, profile your code.

@RogerConnell, here was where I tested out try … except with a simple division example.

I assume if you don’t catch an exception in your code it bubbles out and terminates the show? or am I just typing tired?

In any case, maybe it might be something to play with.

My understanding is that Win32, try-finally and try-except are really expensive. In Win64, try-except is a lot less expensive, as MS designed them better.

But like Vincent: Disclaimer - not an expert… just my experience. If performance really matters, profile your code.