Try/Catch - Library

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Try/Catch - Library

Post 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 !
SPAMINATOR NR.1
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Very usefull !
Thank you.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post 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 .
SPAMINATOR NR.1
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Nice Lib Rings!! Thanks for sharing this. Great work!

Cheers
Mike
Tranquil
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post 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
Image Image
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Try/Catch - Library

Post 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
:?:
DominiqueB
Enthusiast
Enthusiast
Posts: 103
Joined: Fri Apr 25, 2003 4:00 pm
Location: France

There is one !

Post 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.
Dominique

Windows 10 64bits. Pure basic 32bits
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: Try/Catch - Library

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: There is one !

Post 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).
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post 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 .
SPAMINATOR NR.1
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post 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
SPAMINATOR NR.1
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Try/Catch - Library

Post by jassing »

Any chance anyone has the zip file still? or links that work?
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2139
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Try/Catch - Library

Post 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
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Try/Catch - Library

Post by jassing »

Thanks -- will give it a go -- I found my sourcecode version of try/catch was generating failures on win7...

-j
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Try/Catch - Library

Post by Tenaja »

Post Reply