Try/Catch/Finally error handling...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

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

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

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

Post by Mijikai »

skywalk wrote: Let me know when this app reports back. :lol:
Width rsp it should work ;)
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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)))
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

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

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

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

Post 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
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

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

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

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

Post 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)))
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post by skywalk »

Yes, this is to avoid having the same End_Graceful() code within every procedure using OnErrorResume(). :(
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

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

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post by skywalk »

Good find. I am buried too. This could go in feature requests but it is Windows only. :(
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

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

Post 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
Simon White
dCipher Computing
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

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

Post by jacdelad »

The network connection is open until you receive #PB_NetworkEvent_Disconnect. With proper event handling there shouldn't be a problem.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Dudemeister
User
User
Posts: 29
Joined: Sat Oct 26, 2019 6:48 pm

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

Post by Dudemeister »

I had completely forgotten about this forum. I dumped PureBasic 3 years ago and moved on to another development environment.
Post Reply