[Gambas-user] TRY/CANCEL

Benoit Minisini gambas at ...1...
Fri Mar 30 22:57:51 CEST 2007


On vendredi 30 mars 2007, Leonardo Miliani wrote:
> 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.

A few remarks:

- This is 'CATCH', not 'CATCH:'

- The FINALLY section is executed before the CATCH section. So do not use 
RETURN in it, otherwise the CATCH section is not executed.

Regards,

-- 
Benoit Minisini




More information about the User mailing list