[Gambas-user] TRY bug example

Benoit Minisini gambas at ...1...
Sat Dec 10 11:10:58 CET 2005


On Saturday 10 December 2005 08:21, nando wrote:
> Benoit,
>
> This is causing me a bit of trouble..and narrowed it down.
>

First, read carefully the documentation about Val(), Str$(), and CStr(). The 
last function *does not* use localisation settings.

>
> Example 1:
> -------------------------------------------------------------
> PUBLIC SUB Form_Open()
>
> PRINT ERROR
> TRY RMDIR "/home/stupid"    'make sure stupid does not exist
> PRINT ERROR
> PRINT "HELLO: " & ERROR & " is the error"
> TRY RMDIR "/home/stupid"
> TRY PRINT "HELLO: " & ERROR & " is the error"
>
> END
>
>
>
> Output I get on Console:
> -------------------------------------------------------------
> False
> True
> HELLO: T is the error   <---  Error shown as T and not True
> HELLO:  is the error    <---  The T or F value for ERROR is missing
>
>
>
> Example 2:
> -------------------------------------------------------------
> Also, include TRY on the first and third lines and the output
> gets more wierd...
>
> PUBLIC SUB Form_Open()
>
> TRY PRINT ERROR
> TRY RMDIR "/home/stupid"
> TRY PRINT ERROR               'Error should be true from previous line
> PRINT "HELLO: " & ERROR & " is the error"
> TRY RMDIR "/home/stupid"
> TRY PRINT "HELLO: " & ERROR & " is the error"
>
> END
>
> Output is...
> ------------------------------------------------------------
>
> False
> False            <---- I expected True ...see question 3 below.
> HELLO:  is the error  <--- What is the error?
> HELLO:  is the error
>
>
> --------------------------------------------------------------------
> Questions ??
> (1) Why does ERROR print out as False or True but sometimes is T or F ??

Because PRINT use Str$() to convert boolean values to strings, whereas the '&' 
operator use CStr().

> (2) Why is ERROR blank sometimes ??

Because CStr(False) returns NULL.

> (3) Is printing ERROR within a TRY statement showing the previous ERROR ?
>     I'm thinking TRY PRINT can never make ERROR TRUE so that PRINT
> shouldn't affect ERROR.

TRY clears the internal error flag before executing its statement.

> (4) Is it mandatory to ERROR.CLEAR after every TRY ??

Usallay, you use TRY and read the ERROR flag just after. So it is not 
mandatory, and moreover it is useless!

> (5) or does TRY clear ERROR after the next statement ??

See (3).

>
> I'm thinking ERROR should always be T/F or always True/False but not mixed.

Mixed ??? ERROR returns a boolean value, that's all.

> I get better consistent results with STR$(ERROR) but I didn't think I
> needed to force conversion.

See (1), and read the documentation about Str$() and CStr().

>
> Thanks in advance.
> -Fernando
>

Regards,

-- 
Benoit Minisini





More information about the User mailing list