Handling Try/Catch

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Handling Try/Catch

Post by c4s »

+1
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Maitre_Kanter
User
User
Posts: 84
Joined: Mon Sep 06, 2010 3:05 pm

Re: Handling Try/Catch

Post by Maitre_Kanter »

+1

(try,catch,finally)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Handling Try/Catch

Post by mk-soft »

With Macro and OnErrorLib

Update 2

Code: Select all

;-TOP

Macro Try
  OnErrorGoto(?LabelCatch#MacroExpandedCount)
EndMacro

Macro Catch
  Goto LabelEndTry#MacroExpandedCount
  LabelCatch#MacroExpandedCount:
EndMacro

Macro EndTry
  LabelEndTry#MacroExpandedCount:
  OnErrorDefault()
EndMacro

;- test
DisableDebugger

MessageRequester("OnError test", "Test 1")
Try
  B = 10
  A = 10 / B
Catch
  A = 0
EndTry
MessageRequester("OnError test", "Test 1: A=" + A)

Try
  PokeS(10, "Hello World") ; Cause a #PB_OnError_InvalidMemory error
Catch
  MessageRequester("OnError test", "The following error happened: " + ErrorMessage())
EndTry

MessageRequester("OnError test", "Test 2")
Try
  B = 0
  A = 10 / B
Catch
  A = 99
  MessageRequester("OnError test", "The following error happened: " + ErrorMessage())
EndTry
MessageRequester("OnError test", "Test 2: A="+ A)

EnableDebugger
Last edited by mk-soft on Sat Apr 29, 2017 1:06 am, edited 1 time in total.
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
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Handling Try/Catch

Post by Mijikai »

mk-soft wrote:With Macro and OnErrorLib

Update

Code: Select all

/code][/quote]

That is not what we need/want - it doesnt give us any information about the exception...
SergeyA
New User
New User
Posts: 8
Joined: Sun Jan 20, 2013 9:09 am

Re: Handling Try/Catch

Post by SergeyA »

mk-soft wrote:With Macro and OnErrorLib

Update

Code: Select all

;-TOP

Macro Try
  OnErrorGoto(?LabelCatch#MacroExpandedCount)
EndMacro

Macro Catch
  Goto LabelEndTry#MacroExpandedCount
  LabelCatch#MacroExpandedCount:
  OnErrorDefault()
EndMacro

Macro EndTry
  LabelEndTry#MacroExpandedCount:
EndMacro

;- test
DisableDebugger

MessageRequester("OnError test", "Test 1")
Try
  B = 10
  A = 10 / B
Catch
  A = 0
EndTry
MessageRequester("OnError test", "Test 1: A=" + A)

Try
  PokeS(10, "Hello World") ; Cause a #PB_OnError_InvalidMemory error
Catch
  MessageRequester("OnError test", "The following error happened: " + ErrorMessage())
EndTry

MessageRequester("OnError test", "Test 2")
Try
  B = 0
  A = 10 / B
Catch
  A = 99
EndTry
MessageRequester("OnError test", "Test 2: A="+ A)

EnableDebugger
To do this, you need to disable the debugger, after which it will not indicate an error in the code.
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Handling Try/Catch

Post by chi »

And again... Thank you very much, mk-soft. :mrgreen:
Et cetera is my worst enemy
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Handling Try/Catch

Post by Marc56us »

Handling Try/Catch :?:

Yes and No
  • Yes because it is an easy way to handle errors
  • No because it's an easy way to program badly
It's like driving a car with your eyes closed and waiting to fall into the ditch or bump into the wall to find out if you're still on the road or not.

The proper way to code is to always check the availability of a resource before using it

I think Fred probably looked at managing exceptions, but it's likely he did not put it in place because it may take longer to compile and increases the size of the exe :?:

(In My Humble Opinion)

:wink:
DarkDragon
Addict
Addict
Posts: 2218
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Handling Try/Catch

Post by DarkDragon »

Marc56us wrote:I think Fred probably looked at managing exceptions, but it's likely he did not put it in place because it may take longer to compile and increases the size of the exe :?:
The size of the exe will always be increased the more code it contains. A feature is a feature, if you think it bloats your exe too much, you simply don't use it. It is optional and really useful for some projects.

Btw should we force handling exceptions like in java or not like in c++? For the first we also need a throws statement at procedures, but I think this cannot be done because you can call procedures via pointer.
bye,
Daniel
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Handling Try/Catch

Post by Sicro »

+1
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