[fpc-pascal] using exceptions
noreply at z505.com
noreply at z505.com
Thu Dec 22 01:19:07 CET 2011
> Hello,
>
> Exceptions like the name suggested, is to mark something that you are not
> suppose to expect.
> For example while getting a data something went wrong, like disconnection,
> wrong size of data etc...
>
> The usage of exception is very costly in performance, and there for should
> use only on specific events imho.
>
> But we use exceptions even on places such as StrToInt, when the string is
> not an integer number, but we can expect to either have a number or
> something else. So why use an exception there ?
>
> What do you think on this subject ? How would you choose when or where to
> use exceptions and why there ?
>
> Ido
This is a difficult question that cost me many nights of sleep.
The TryStrToInt function is better than StrToInt for checking whether the
string is valid number. In this TryStrToInt function you just check for
TRUE or FALSE instead of exceptions. IF you look at the source code of
TryStrToInt in delphi, it just wraps the old Val() procedure.
So let me as this question.. why is it that TryStrToInt is useful if we
have exceptions? isn't it ambiguous to offer people two ways of doing the
same thing? answer: sometimes exceptions really aren't any cleaner than
using regular programming logic to check for errors.
Now another problem with exceptions is that you don't know for sure where
an exception will occur in code, whereas if someone ignores the error
return value, you can see it in the code that they have ignored it.
More information about the fpc-pascal
mailing list