Page 1 of 2

Try/Catch - Library

Posted: Sun Aug 17, 2003 7:44 pm
by Rings
Try/Catch - Library , first version released now!

This new Library (with only 2 commands) is an enhancement to the OnError-library from Purebasic.

Introduction:
If you are familar with Java or C++, you remember the Errorhandling via Try-Catch-Finally Commands.
For Example

Code: Select all

Try{
   CopyMemory();}
Catch{
   printf("error");}
Finaly{};
This Try-Catch-Construct works in that way:
If CopyMemory fails, the Printf-procedure is called.

And that is the same way it works in Purebasic now.

Code: Select all

If TryError()
   CopyMemory(1,0,10)
else
   Messagerequester("Info","CopyMemory fails",0)
endif:EndTryError()

The TryError() and EndTryError() can be interlocked.

There is no real Helpffile available for those 2 new commands, but they are easy to use .

Download: http://home.pages.at/purefan/libraries/ ... ycatch.zip

Another example:

Code: Select all

 
If TryError() ;The Try-section begins 

  Null.l=0 
  Ergebnis.l = 123 / Null 

Else  ;Catch-Section, 
  Debug "Division by zero!" 

EndIf:EndTryError() 

we hope to the future,that if Purebasic has Macro-capacities, this commands can been renamed to the originals.
Using the library is easy, less bloated(1Kb in exe) and its freeware (Pure 3.7.x is recommented)
The TryCatch-Library is written and (c) by PureFan and some Parts by me ,Siegfried Rings . Have fun tackling Errors !

Posted: Mon Aug 18, 2003 7:41 am
by gnozal
Very usefull !
Thank you.

Posted: Mon Aug 18, 2003 11:04 am
by Rings
if you encountered any problems, please post a message here.Else this lib will been officall and in 2 weeks posted to the resourcesite and purearea.net .

Posted: Tue Aug 19, 2003 6:42 am
by Tranquil
Nice Lib Rings!! Thanks for sharing this. Great work!

Cheers
Mike

Posted: Sat Aug 23, 2003 6:30 am
by Paul
Very nice library Rings.
The problem I have is that it does not function correctly in a Procedure ;(

Try this:

Code: Select all

Procedure.l Test()
  Result=0
  If TryError() ;This line starts the Try-Section
    Result+1
    Debug "Just entered the try section"    
    CopyMemory(1,2,1)
    Debug "This line will never execute"
 
    Else
    Result+2
    Debug "Just entered the catch section"
  EndIf:EndTryError()
  
  ProcedureReturn Result
EndProcedure
                      
                      
Debug Test()
It should return something other than 0

Re: Try/Catch - Library

Posted: Sat Aug 23, 2003 9:04 am
by PB

Code: Select all

If TryError()
   CopyMemory(1,0,10)
else
   Messagerequester("Info","CopyMemory fails",0)
endif:EndTryError()
I don't understand... what's the difference between the above, and this:

Code: Select all

If CopyMemory(1,0,10)=0
  Messagerequester("Info","CopyMemory fails",0)
EndIf
:?:

There is one !

Posted: Sat Aug 23, 2003 9:40 am
by DominiqueB
The main difference is that when running as an .exe, if you fall into fatal error (ex: div by 0) with "try" you keep control on your prog and can manage the error.
Without it your app will simply crash !

That makes a big difference for me...

Dominique.

Re: Try/Catch - Library

Posted: Sat Aug 23, 2003 9:45 am
by sec
I don't understand... what's the difference between the above, and this:
i don't have soure of try/cacth but think this lib use SEH handle,
i also see a good link about SEH :here

HTH

Re: There is one !

Posted: Sat Aug 23, 2003 2:30 pm
by PB
Okay, I now understand that the Try library will stop a fatal error, but
doesn't that mean heaps of Try...EndTry routines in your code? Why
not, for example, just check for div/0 manually, etc? I don't see any
real advantages to doing normal error-checking by the programmer?

For example, why are you doing this:

Code: Select all

If TryError() ;The Try-section begins 
  Null.l=0 
  Ergebnis.l = 123 / Null 
Else  ;Catch-Section, 
  Debug "Division by zero!" 
EndIf:EndTryError() 
Instead of this:

Code: Select all

Null.l=0
If Null=0
  Debug "Division by zero!"
Else
  Ergebnis.l = 123 / Null
EndIf
Seems like a lot of extra typing when using Try...EndTry?

(BTW Rings: I'm not trashing your library, I'm just genuinely interested
in why it needed to be written in the first place).

Posted: Sat Aug 23, 2003 4:46 pm
by Rings
Paul: i will take a look, seems a stack-issue
Sec: Of course its SEH (Structured ExceptionHandling), also in the OnErrorLib
PB: there are sometimes moments in codings where not all errors are catched before with an IF Endif
Also take a look into the OnErrolib, which is in some cicumstance better than this TryCatch construct.
And you're right, better coders check their functions-result.so this is sometimes usefull for the lazy-ones .

Posted: Mon Aug 25, 2003 1:17 pm
by Rings
As for the stack-problem(thx paul for the report), its fixed now.
So this lib can also been used in a procedure

download at

http://home.pages.at/purefan/libraries/ ... ycatch.zip

same adress as above

Re: Try/Catch - Library

Posted: Tue Jul 31, 2012 7:18 pm
by jassing
Any chance anyone has the zip file still? or links that work?

Re: Try/Catch - Library

Posted: Tue Jul 31, 2012 7:38 pm
by Andre
jassing wrote:Any chance anyone has the zip file still? or links that work?
From my website:
http://www.purearea.net/pb/download/use ... ycatch.zip

:D

Re: Try/Catch - Library

Posted: Tue Jul 31, 2012 10:03 pm
by jassing
Thanks -- will give it a go -- I found my sourcecode version of try/catch was generating failures on win7...

-j

Re: Try/Catch - Library

Posted: Tue Jul 31, 2012 11:44 pm
by Tenaja