Page 2 of 2

Re: Handling Try/Catch

Posted: Thu Apr 27, 2017 7:26 pm
by c4s
+1

Re: Handling Try/Catch

Posted: Fri Apr 28, 2017 1:39 pm
by Maitre_Kanter
+1

(try,catch,finally)

Re: Handling Try/Catch

Posted: Fri Apr 28, 2017 5:44 pm
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

Re: Handling Try/Catch

Posted: Fri Apr 28, 2017 7:27 pm
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...

Re: Handling Try/Catch

Posted: Fri Apr 28, 2017 9:52 pm
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.

Re: Handling Try/Catch

Posted: Fri Apr 28, 2017 11:33 pm
by chi
And again... Thank you very much, mk-soft. :mrgreen:

Re: Handling Try/Catch

Posted: Sun Apr 30, 2017 10:08 am
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:

Re: Handling Try/Catch

Posted: Mon May 01, 2017 8:10 am
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.

Re: Handling Try/Catch

Posted: Mon May 01, 2017 12:01 pm
by Sicro
+1