Try-Catch

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Try-Catch

Post by remi_meier »

Hi!
I don't know if I posted this before, but it's really important for me! I
would like to have Try-Catch implemented in PureBasic because for
real error-handling, there is no alternative! Look at the example:

Code: Select all

Procedure dangerous()
  Dim a.l(50)
  NewList k.l()
  For z = 0 To 99999
    AddElement(k())
  Next
  CopyMemory(1, 1, 1)
  
EndProcedure

OnErrorGoto(?error)
MessageRequester("Start", "Start the test.") ;2667KB
If #True;TryError()
  dangerous()
Else
  error:
  MessageRequester("Error", "There was an error!") ;5080KB
EndIf; : EndTryError()
As you can see with the taskmanager, the amount of memory usage
grows while calling the procedure (see comments after MessageReq.()).
When an error occurs and the error handler jumps out of the procedure
the memory of the local lists and arrays won't be freed and you shouldn't
run the program any longer as this could create some errors. Furthermore
there isn't a safe variant of CopyMemory(), that means, I cannot be sure
if there is also some unfreed memory used by CopyMemory(), I would
like to have a CopyMemorySafe() function.

What I would like to have is, that I could use SetErrorNumber() in a
procedure without needing to have the error-handling within that procedure (like Try-Catch, not OnErrorGoSub()).

The PB-compiler should just have a "safe" mode in which the compiler
jumps to the end of the procedure when an exception occurs to free all
the unused memory and only then jump to the Catch-section.

Yes, I know this won't be implemented soon as it could be a big job if you
want it to work the right way (and you surely want). Just a wish... :wink:

greetz
Remi
Athlon64 3700+, 1024MB Ram, Radeon X1600
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

IIRC, Rings did a try/catch user lib, may be it will fit your needs ?
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Yes, I rewrote it for PBOSL :wink:
It's much easier but has the same problems when leaving procedures.
Athlon64 3700+, 1024MB Ram, Radeon X1600
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

remi_meier wrote:Yes, I rewrote it for PBOSL :wink:
:lol:
Post Reply