Simple Try/Catch (As long as it is not integrated yet)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Simple Try/Catch (As long as it is not integrated yet)

Post by mk-soft »

From Features Requests... :wink:

Code: Select all

;- Try/Catch by mk-soft version v1.2

#State_IsDebugger = #PB_Compiler_Debugger

Macro Try
  CompilerIf #State_IsDebugger
    DisableDebugger
  CompilerEndIf
  OnErrorGoto(?LabelCatch#MacroExpandedCount)
EndMacro

Macro Catch
  Goto LabelEndTry#MacroExpandedCount
  LabelCatch#MacroExpandedCount:
  OnErrorDefault()
  CompilerIf #State_IsDebugger
    EnableDebugger
  CompilerEndIf
EndMacro

Macro EndTry
  LabelEndTry#MacroExpandedCount:
  OnErrorDefault()
  CompilerIf #State_IsDebugger
    EnableDebugger
  CompilerEndIf
EndMacro

;- Test

Try
  B = 100
  A = B / 10
Catch
  A = 0
EndTry
Debug A

Try
  B = 0
  A = 10 / B
Catch
  Debug ErrorMessage()
  A = 9999  
EndTry
Debug A
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Simple Try/Catch (As long as it is not integrated yet)

Post by Sicro »

Nice improvements :)

But as with the codes in the german forum thread, this try-catch workaround has problem with the following code, too:

Code: Select all

Try
  CallFunctionFast(50)
Catch
  MessageRequester("Error", "An error has occurred!")
EndTry
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Simple Try/Catch (As long as it is not integrated yet)

Post by JHPJHP »

Hi mk-soft,

You have exceptional programming skills :!:

-----------------------------------------

Here is a Windows Only version by Danilo:
- [Windows XP+] Try .. Catch .. EndTry - Error Handling
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simple Try/Catch (As long as it is not integrated yet)

Post by mk-soft »

Many Thanks,

Try to stay fit in Purebasic. In the work I mostly only with SPS programming or VB script to do.
It rarely happens that I write with Purebasic for industrial applications or services programs.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simple Try/Catch (As long as it is not integrated yet)

Post by mk-soft »

Sicro wrote:Nice improvements :)

But as with the codes in the german forum thread, this try-catch workaround has problem with the following code, too:

Code: Select all

Try
  CallFunctionFast(50)
Catch
  MessageRequester("Error", "An error has occurred!")
EndTry
For this case, we have to look at how we can save all register before and restore it in the event of a failure
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Simple Try/Catch (As long as it is not integrated yet)

Post by Sicro »

All Try-Catch workarounds via macros and the PB-OnErrorLib should be viewed only for the fun of programming and should not be used in productive use because they are not safe to use:
The program stack will not be adjusted before jumping to the label, so local variables should not be accessed as they may not be reachable anymore. It is also not safe to continue normal program execution after an error as things like the return address of a procedure may be wrong if the stack is no longer correct. The best practice is just to gather and display information about the error and then exit the program.
Source: http://purearea.net/pb/english/manual/o ... rgoto.html

I think the best way we can go currently is to manipulate the asm output from pb before fasm gets it. It's the way the forum member Rings went, if I'm not mistaken.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply