Re: Handling Try/Catch
Posted: Thu Apr 27, 2017 7:26 pm
+1
http://www.purebasic.com
https://www.purebasic.fr/english/
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
mk-soft wrote:With Macro and OnErrorLib
UpdateCode: Select all
/code][/quote] That is not what we need/want - it doesnt give us any information about the exception...
To do this, you need to disable the debugger, after which it will not indicate an error in the code.mk-soft wrote:With Macro and OnErrorLib
UpdateCode: 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
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.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![]()