[fpc-pascal] using exceptions

Bernd prof7bit at googlemail.com
Tue Dec 20 07:53:55 CET 2011


2011/12/19 Marcos Douglas <md at delfire.net>:

>> The usage of exception is very costly in performance, and there for should
>> use only on specific events imho.

IMHO it has its legitimate uses. Thee are even languages (Python for
example) where it is considered totally normal to use exceptions all
over the place all the time, for example even every for loop will be
terminated (internally) by raising and catching an ent-of-iterator
exception, files raise an end-of-file exception, etc.

IMHO if you have something like

try
  foo := StrToInt(Argv[1]);
except
  // I don't care why exactly it fails
  foo := DEFAULT_FOO;
end;

then it makes the code much simpler and nothing can go wrong and also
I don't see any performance penalty here, it happens only once on
program start and not in a tight loop, in cases like this it is IMHO
totally ok.

It only becomes dangerous when you overuse it across larger and more
deeply nested parts of the code, for example where Classes might get
instantiated within the try and even more complicated is when
exceptions are allowed to happen in your constructors after it has
already constructed half (but not all) of its child objects, then you
have to take care that everything (even the incompletely constructed
objects) is properly destructed again,  this possibility can make your
destructors more complicated (or you are forced to use reference
counted interfaces for everything) and in the end the code won't get
simpler but more confusing instead.

Bernd



More information about the fpc-pascal mailing list