[Lazarus] Assignment on exception (was Re: exception handling in constructor)

Sven Barth pascaldragon at googlemail.com
Sat Mar 2 14:32:46 CET 2013


On 02.03.2013 14:04, Xiangrong Fang wrote:
> Hi,
>
> Could anyone explain this:
>
> Number := 123;
> try
>    Number := Ln(0);
> except
> end;
> WriteLn(Number);
>
> The output is *Nan*, not 123. i.e. when exception occurs the variable is
> modified anyway!  So in the example below assigning MyInstance to nil
> before Create does not help to ensure it is nil if an exception occurs?

Because there is no exception. If you put a "Writeln" or something into 
the except block you'll see that no exception is raised. Additionally 
the "ln" is a compiler intrinsic. And as you give it a constant 0 the 
compiler will hardcode the "NaN" value instead of calculating it.

The safest way in this case is to put the "Number := 123" into the 
"except" block (though in this specific example it wouldn't change 
anything).

Regards,
Sven




More information about the Lazarus mailing list