Page 3 of 4

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 4:30 pm
by Dawgmann
"Let me know when this app reports back."

I'm not sure what your issue is. The function returns the value (404) to the calling procedure while allowing me to acquire the error number via ErrorCode() and the error message via ErrorMessage(). Exactly what I wanted it to do.

Some people just work so hard at making issues more difficult than they need to be.

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 4:51 pm
by Mijikai
skywalk wrote: Let me know when this app reports back. :lol:
Width rsp it should work ;)

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 6:28 pm
by skywalk
Nice try, but Fails with threadsafe.
Dawgmann wrote:Some people just work so hard at making issues more difficult than they need to be.
Sorry if my "toy" apps are a problem.

Code: Select all

; Mijikai, https://www.purebasic.fr/english/viewtopic.php?f=3&t=73592&start=15
; COMPILER OPTIONS:
;   [ ] Main source file:
;   [ ] Use Compiler:   PureBasic 5.71 (Windows - x64)
;   [ ] Use Icon:       
;   [ ] Enable inline ASM syntax coloring
;   [x] Create threadsafe executable
;   [x] Enable OnError lines support
;   [x] Enable modern theme support (for Windows XP and above)
;   [ ] Request Administrator mode for Windows Vista and above
;   [ ] Request User mode for Windows Vista (no virtualisation)
;   [ ] Enable DPI aware executable (Windows)
;   Library Subsystem:
;   Executable format:        Windows ;|Console|Shared DLL
;   CPU Optimisation:         All     ;|Dynamic|w/MMX|w/3DNOW|w/SSE|w/SSE2
;   Sourcefile Text encoding: UTF-8
; COMPILE/RUN:
;   [x] Create temporary executable in the source directory
; CONSTANTS:
; VERSION INFO/RESOURCES:
;   Update version info in:   auto set fields --> %yy.%mm.%dd OR C:\dev\some-resource.rc
; IDE PREFERENCES:ToolsPanel:Configuration:Procedure Browser
;   [x] Sort Procedures by name       ;<-- Alphabetical instead of order of appearance. Uncheck when location of Proc desired.
;   [ ] Group Markers                 ;<-- If checked, ';-!' priority listing is lost.
;   [ ] Display Procedure Arguments   ;<-- Too busy if checked.
#mysmall1 = 1e-2
Macro OnErrorResume(Label)
  !push rax rcx rdx rbx rbp rsi rdi rsp
  OnErrorGoto(Label)
EndMacro
Procedure End_Graceful()
  MessageRequester("End Graceful", ErrorMessage(ErrorCode()))
EndProcedure
Procedure.i Dummy(Something.i)
  OnErrorResume(?ErrorHandler)
  Protected.i x,y
  x = 1 / (y + #mysmall1)   ; FAIL
  ProcedureReturn Something
  ErrorHandler:
  End_Graceful()
EndProcedure
MessageRequester(#Null$, Str(Dummy(123)))

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 6:45 pm
by Dawgmann
Yeah...

I don't care if it fails on Threadsafe. I don't care if it fails when dividing by zero. I don't care if it fails when unicorns duck behind a tree to take a poop.

I need it for what I do. The fact you can't comprehend that after all my posts saying so, and after all this time, well... that kind of eludes to possible mental issues and your Trump-like need to feel like your correct.

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 7:00 pm
by Mijikai
@skywalk

Code: Select all

Macro OnErrorResume(Label)
  !push rax rcx rdx rbx rbp rsi rdi rsp r8 r9 r10 r11 r12
  OnErrorGoto(Label)
EndMacro

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 7:08 pm
by skywalk
Dawgmann wrote:Yeah...

I don't care if it fails on Threadsafe. I don't care if it fails when dividing by zero. I don't care if it fails when unicorns duck behind a tree to take a poop.

I need it for what I do. The fact you can't comprehend that after all my posts saying so, and after all this time, well... that kind of eludes to possible mental issues and your Trump-like need to feel like your correct.
You continue to disparage those trying to help you? You said you MUST capture unintended errors and exit gracefully. Yet, now you say, some errors are ok to crash away. I only show divide by zero because it is a simple case. My apps are always threaded, so I showed that too. There are many other error types like stepping beyond array limits or null pointers and so on. By all means, shout me down for Trump-like help.

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 7:13 pm
by skywalk
Mijikai wrote:@skywalk

Code: Select all

Macro OnErrorResume(Label)
  !push rax rcx rdx rbx rbp rsi rdi rsp r8 r9 r10 r11 r12
  OnErrorGoto(Label)
EndMacro
Sorry, this immediately triggers an error even with divide by zero commented out :shock:

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 7:40 pm
by Mijikai
skywalk wrote:
Mijikai wrote:@skywalk

Code: Select all

Macro OnErrorResume(Label)
  !push rax rcx rdx rbx rbp rsi rdi rsp r8 r9 r10 r11 r12
  OnErrorGoto(Label)
EndMacro
Sorry, this immediately triggers an error even with divide by zero commented out :shock:
True i need to look into it.

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 7:49 pm
by Mijikai
I assume we just need to modify the return address:

Code: Select all

Macro OnErrorResume(Label)
  OnErrorGoto(Label)
EndMacro

Procedure End_Graceful()
  MessageRequester("End Graceful", ErrorMessage(ErrorCode()))
EndProcedure

Procedure.i Dummy(Something.i)
  OnErrorResume(?ErrorHandler)
  Protected.i x,y
  PokeS(10, "Hello World")
  ProcedureReturn Something
  ErrorHandler:
  !push rax rcx rdx rbx rsi rdi r8 r9;<- this works also here
  
  End_Graceful();<- this jumps back to the old code
  
  ;if we figure out how to patch the return address everything will work i guess
  
  ProcedureReturn 444
EndProcedure
MessageRequester(#Null$, Str(Dummy(123)))

Re: Try/Catch/Finally error handling...

Posted: Thu Sep 12, 2019 8:03 pm
by skywalk
Yes, this is to avoid having the same End_Graceful() code within every procedure using OnErrorResume(). :(

Re: Try/Catch/Finally error handling...

Posted: Fri Sep 13, 2019 10:13 am
by Mijikai
Its probably better to build it from scratch.

Something like this: https://www.codeproject.com/articles/17 ... -vista-x64
Remember the old days when you set the SEH in your code?
Well, with x64/Itanium they're gone.
Exception Handlers are now stored as structured in the PE64 Exception Directory.
If you need to generate code dynamically and set for it an exception handler, you can use the function RtlAddFunctionTable, which takes as parameter an array of RUNTIME_FUNCTION structures. This means, of course, that you'll have to fill one or more UNWIND_INFO structure/s by yourself.
Other links:
https://docs.microsoft.com/en-us/cpp/bu ... ew=vs-2019
http://www.osronline.com/article.cfm%5earticle=469.htm
https://stackoverflow.com/questions/285 ... ctiontable

Some (hopefully) translated structs:

Code: Select all

EnableExplicit

Enumeration UNWIND_OP_CODES
  #UWOP_PUSH_NONVOL = 0 ;info == register number
  #UWOP_ALLOC_LARGE     ;no info, alloc size in Next 2 slots
  #UWOP_ALLOC_SMALL     ;info == size of allocation / 8 - 1
  #UWOP_SET_FPREG       ;no info, FP = RSP + UNWIND_INFO.FPRegOffset*16
  #UWOP_SAVE_NONVOL     ;info == register number, offset in Next slot
  #UWOP_SAVE_NONVOL_FAR ;info == register number, offset in Next 2 slots
  #UWOP_SAVE_XMM128 = 8 ;info == XMM reg number, offset in next slot
  #UWOP_SAVE_XMM128_FAR ;info == XMM reg number, offset in next 2 slots
  #UWOP_PUSH_MACHFRAME  ;info == 0: no error-code, 1: error-code
EndEnumeration

#UNW_FLAG_EHANDLER = $1
#UNW_FLAG_UHANDLER = $2
#UNW_FLAG_CHAININFO = $4

Structure RUNTIME_FUNCTION Align 4
  BeginAddress.l
  EndAddress.l
  UnwindData.l
EndStructure

Structure UNWIND_CODE_INFO
  CodeOffset.a
  UnwindOp.a[4]
  OpInfo.a[4]
EndStructure

Structure UNWIND_CODE
  StructureUnion
    Info.UNWIND_CODE_INFO
    FrameOffset.u
  EndStructureUnion
EndStructure

Structure UNWIND_INFO Align 4
  Version.a[3]
  Flags.a[5]
  SizeOgProlog.a
  CountOfCodes.a
  FrameRegister.a[4]
  FrameOffset.a[4]
  UnwindCode.UNWIND_CODE[0]
EndStructure

;-> https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019
;-> On runtime we could use: RtlAddFunctionTable_()
Im not sure how to actually do it yet, so unless someone wants to help with this
i rather invest my time in something different.

Re: Try/Catch/Finally error handling...

Posted: Fri Sep 13, 2019 3:00 pm
by skywalk
Good find. I am buried too. This could go in feature requests but it is Windows only. :(

Re: Try/Catch/Finally error handling...

Posted: Mon Sep 26, 2022 3:51 pm
by swhite
Hi

The IsXXX(1) functions are very useful I just wish there was one for IsNetworkConnection(). There a times when I open a connection to a server and something goes wrong on the server and the connection is lost then I get a Segmentation fault if I use the Connection. It would be nice if it were possible to know if the connection is still valid.

Simon

Marc56us wrote: Tue Sep 10, 2019 4:24 pm If PB is fast and light, it is precisely because he doesn't bother with all these checks.

There are only two public for PB
  • Beginners (who do not handle user errors)
  • Experienced (sometime professionals) who know how to handle errors without "trial and error" (try/catch)
The "On Error" lib and the IsXXX(1) functions are sufficient to make stable programs. All native PB functions return a value.

And we've been doing this for over 20 years in PB :wink:

(1) IsBillboardGroup, IsCamera, IsDatabase, IsDialog, IsDirectory, IsEffect, IsEntity, IsFile, IsFingerprint, IsFont, IsFtp, IsGadget, IsGadget3D, IsImage, IsInfinity, IsInsidePath, IsInsideStroke, IsJSON, IsJoint, IsLibrary, IsLight, IsMail, IsMaterial, IsMenu, IsMesh, IsMovie, IsMusic, IsNaN, IsNode, IsParticleEmitter, IsPathEmpty, IsProgram, IsRegularExpression, IsRuntime, IsScreenActive, IsSerialPort, IsSound, IsSound3D, IsSprite, IsStaticGeometry, IsStatusBar, IsSysTrayIcon, IsText3D, IsTexture, IsThread, IsToolBar, IsWindow, IsWindow3D, IsXML

Re: Try/Catch/Finally error handling...

Posted: Mon Sep 26, 2022 5:55 pm
by jacdelad
The network connection is open until you receive #PB_NetworkEvent_Disconnect. With proper event handling there shouldn't be a problem.

Re: Try/Catch/Finally error handling...

Posted: Mon Sep 26, 2022 6:31 pm
by Dudemeister
I had completely forgotten about this forum. I dumped PureBasic 3 years ago and moved on to another development environment.