Limits of OnErrorCall

Just starting out? Need help? Post your questions and find answers here.
sc4pb
User
User
Posts: 32
Joined: Tue Mar 07, 2023 5:33 pm

Limits of OnErrorCall

Post by sc4pb »

I have an application that crashes, as in completely disappears with no errors, no warning, even though I have OnErrorCall() in place. I was hoping OnErrorCall allowed for unexpected errors to be trapped in a handler so I could do a graceful exit and log the event.

The application creates a PDF report. I had an accidental situation where a bad filename could be supplied to PdfVectorOutput(), causing StartVectorDrawing() to then fail. After that a call to VectorFont() causes the entire program to crash with no warning. (Yes, yes, I was checking result of StartVectorDrawing, and it was a mistake on my part that the program was continuing to render the report.)

But a bad filename, or hard drive full, or a networking error, these are all good examples of situations that could arise unexpectedly, and I assumed that as a worst case an error handler would allow the event to be logged, so that the root cause could be fixed.

So... why does the following OnErrorCall example not work, and what exactly are the limits to what type of errors can be trapped?

(I compiled below to an .exe on Windows 10, PB 6.12 with "Enable Debugger" unchecked in compiler options.)

Code: Select all

Procedure VectorErrorOnPurpose()
  
  
  LoadFont(0,"Times New Roman",20)
  VectorFont(FontID(0),18)
  
EndProcedure


Procedure ErrorHandler()
  
  MessageRequester("test","Error trapped successfully!")
  
EndProcedure


MessageRequester("test","Enable Error trapping")
OnErrorCall(@ErrorHandler())

MessageRequester("test","Causing error...")
VectorErrorOnPurpose()
MessageRequester("test","Should never see this!")
Thanks
User avatar
mk-soft
Always Here
Always Here
Posts: 6224
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Limits of OnErrorCall

Post by mk-soft »

Programming errors are not intercepted.
Here you must always also evaluate the return from the PB functions themselves.

The following errors are started by OnErrorCall. See function ErrorCode()

Code: Select all

  #PB_OnError_InvalidMemory         : Read or write operation on an invalid location
  #PB_OnError_Floatingpoint         : Floating-point error
  #PB_OnError_Breakpoint            : Debugger breakpoint reached (non-PureBasic breakpoints)
  #PB_OnError_IllegalInstruction    : Attempt to execute an illegal instruction
  #PB_OnError_PriviledgedInstruction: Attempt to execute a privileged (system-) instruction
  #PB_OnError_DivideByZero          : Division by zero (Windows only)
Valid procedure:

Code: Select all

filename.s = SaveFileRequester("PDF", "", "", 0)
If filename
  vector_output = PdfVectorOutput(filename, 800, 1200, #PB_Unit_Pixel)
  If vector_output
    If StartVectorDrawing(vector_output)
      ; todo
      StopVectorDrawing()
    Else
      Debug "Error: StartVectorDrawing"
    EndIf
  Else
    Debug "Error: PdfVectorOutput to file " + filename
  EndIf
Else
  Debug "Cancel."
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Limits of OnErrorCall

Post by BarryG »

Always check the return value of commands that give one. Always. For example, a bad filename error should never occur, because you used the CheckFilename() command with it first. Things like that.
User avatar
Piero
Addict
Addict
Posts: 884
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Limits of OnErrorCall

Post by Piero »

BarryG wrote: Wed Jul 16, 2025 10:01 pmCheckFilename()

Code: Select all

Procedure.s QuoteString(str$) ; useful to quote FILE/FOLDER PATHS (Mac/Linux Shell)
   ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
EndProcedure
sc4pb
User
User
Posts: 32
Joined: Tue Mar 07, 2023 5:33 pm

Re: Limits of OnErrorCall

Post by sc4pb »

Okay let me ask another way...

The OnError system is apparently not what I'm looking for, now that I understand its scope.

There is also the debugging system. In the event of a programming error (a bug! yes, we all let one escape into the wild now and then), If I compile a program with debugging enabled, and deploy it to a customer, is there a way to, in the event of a bug like the one I described, to toss execution to a handler routine before exiting?

Execution speed is not a concern. I don't want the full PB debugger in the field. But clearly the debugging system is capable of identifying errors of this kind because that's exactly what it does when you run from the IDE. Maybe this is a wishlist item: I'd like a way, through debugger library or whatever, that when a compiled and deployed program runs into an unexpected programming mistake, instead of just folding up and crashing, toss execution to a handler, so that a message like "an unexpected error has occurred, please contact..." could be displayed.

Thanks.
Little John
Addict
Addict
Posts: 4780
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Limits of OnErrorCall

Post by Little John »

<deleted>
Last edited by Little John on Thu Jul 17, 2025 6:55 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Limits of OnErrorCall

Post by Fred »

if you compile in console mode, you can enable the debugger and ship it with it, but it shouldn't be put in production IMHO as it contains info about the source file name, etc.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Limits of OnErrorCall

Post by PBJim »

Fred wrote: Thu Jul 17, 2025 6:49 pm if you compile in console mode, you can enable the debugger and ship it with it, but it shouldn't be put in production IMHO as it contains info about the source file name, etc.
I have some sympathy for the requirement — the PB debugger serves a very good role for highlighting certain errors that we might not necessarily identify otherwise, especially memory handling errors. In this sense the console debugger could be a possible option with a collaborating end-user, when working together to try to resolve an illusive and difficult bug. :D

Image

I don't see the need to add a feature though, as the post perhaps suggests — it's part of the need for adequate testing before releasing and we can do the above already.
Post Reply