[Gambas-user] TRY/CANCEL

Leonardo Miliani leonardo at ...1237...
Fri Mar 30 22:51:52 CEST 2007


Vince Scott (SBC) ha scritto:
> Hi All,
> 
>  
> 
> I need some information on the TRY/CATCH error trapping in Gambas. How do I
> TRY/CATCH a block of code? I don't need to use a TRY and CATCH on each line
> do I?
> 
>  
> 
> Any information would be appreciated.
> 
>  
> 
> Vince Scott
> 

It should work like belows:

---------------------------
Public Function Test() as String
Dim A as integer

A="ooooo"
RETURN A

CATCH:
Message.Error("An error is occured while assigning string to variable.")
END
----------------------------

Gambas will "try" to execute the specified instruction: if an error
occurs, then the CATCH instance is executed.
You can also add the FINALLY instruction before CATCH: in this case, the
code betweem FINALLY and CATCH will be ever excecuted:

-----------------------
Public Function Test() as String
Dim A as integer
Dim B as string

A="ooooo"
B="ppppp"
FINALLY
RETURN B

CATCH:
Message.Error("An error is occured while assigning string to variable.")
END
------------------------

In this function, an error will occur then Gambas will try to assign a
string to the variable A that can only contains integers. But, after the
error message, the function will return "ppppp" in both the cases
because of the FINALLY istruction.

The last case is the use of TRY instruction. With TRY you can tell the
Gambas interpreter not to raise an error but manage it.
Here an example:

----------------------
[..code..]
TRY OPEN Filename FOR INPUT AS #hFile
IF ERROR THEN Message.Error("File does not exist")
[..code..]
---------------------

Gambas will "try" to execute the instruction or command after the TRY:
if it can't, ERROR will be true and you'll be able to manage the error
using the Error class.

-- 
Ciao.
Leo.

Web: www.leonardomiliani.com
E-mail: leonardo at ...1237...
Scegli software opensource - Choose opensource software




More information about the User mailing list