PureBasic Forum
https://www.purebasic.fr/english/

Try/Catch/Finally error handling...
https://www.purebasic.fr/english/viewtopic.php?f=3&t=73592
Page 3 of 3

Author:  Dawgmann [ Thu Sep 12, 2019 4:30 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

"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.

Author:  Mijikai [ Thu Sep 12, 2019 4:51 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

skywalk wrote:
Let me know when this app reports back. :lol:


Width rsp it should work ;)

Author:  skywalk [ Thu Sep 12, 2019 6:28 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

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:
; 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)))

Author:  Dawgmann [ Thu Sep 12, 2019 6:45 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

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.

Author:  Mijikai [ Thu Sep 12, 2019 7:00 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

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

Author:  skywalk [ Thu Sep 12, 2019 7:08 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

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.

Author:  skywalk [ Thu Sep 12, 2019 7:13 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

Mijikai wrote:
@skywalk
Code:
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:

Author:  Mijikai [ Thu Sep 12, 2019 7:40 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

skywalk wrote:
Mijikai wrote:
@skywalk
Code:
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.

Author:  Mijikai [ Thu Sep 12, 2019 7:49 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

I assume we just need to modify the return address:

Code:
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)))

Author:  skywalk [ Thu Sep 12, 2019 8:03 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

Yes, this is to avoid having the same End_Graceful() code within every procedure using OnErrorResume(). :(

Author:  Mijikai [ Fri Sep 13, 2019 10:13 am ]
Post subject:  Re: Try/Catch/Finally error handling...

Its probably better to build it from scratch.

Something like this: https://www.codeproject.com/articles/17263/moving-to-windows-vista-x64

Quote:
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.


Quote:
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/build/exception-handling-x64?view=vs-2019
http://www.osronline.com/article.cfm%5earticle=469.htm
https://stackoverflow.com/questions/28549775/seh-handlers-using-rtladdfunctiontable

Some (hopefully) translated structs:
Code:
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.

Author:  skywalk [ Fri Sep 13, 2019 3:00 pm ]
Post subject:  Re: Try/Catch/Finally error handling...

Good find. I am buried too. This could go in feature requests but it is Windows only. :(

Page 3 of 3 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/