PxPlus User Forum

Twitter Twitter Twitter

Author Topic: error handling in objects  (Read 759 times)

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
error handling in objects
« on: July 17, 2022, 10:59:56 AM »
If the goal is to capture the minor errors in an object create or a method, is there a way to use SETERR and do a proper return integer/string to the stack?  I am looking to save the errNumber and ErrMessage$ and suppress error processing from an escape err or exit err in the stack program so they can handle the error as required.  I can do this using TRY or EVN/EVS encapsulation but it would require extensive additions to every object method of which there are around 100 objects and unknown qty of methods and anticipating all possible errors is not likely.

Stéphane Devouard

  • Diamond Member
  • *****
  • Posts: 122
  • PxPlus guru with skills in PHP, JS, C#, Java
    • View Profile
    • Stéphane's Web Resume
Re: error handling in objects
« Reply #1 on: July 18, 2022, 06:36:50 AM »
Peter

If you have a common ERROR_HANDLER, maybe you could modify it to detect if _OBJ<>0 which means you are running in an object context
You could then caputre some of the ERR() values in local properties that would be declared in base class with error management routines that you would have to inherit in all your other classes
As for TRY and EVN/EVS encapsulation, have you also checked the TRY() function which can also intercept errors and return default values ?

Hope these general thoughts will help
Stéphane Devouard
Portfolio | Work

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: error handling in objects
« Reply #2 on: July 18, 2022, 11:06:01 AM »
You can include a SETERR in the object definition and it will take effect whenever running in the object.

For Example:

0010 DEF CLASS "a"
0020 SETERR Trap_it
0030 PROPERTY name$
0040 FUNCTION Divide(x)
0050 ENTER x
0060 LET a=100/x
0070 RETURN a
0080 END DEF
0090 !
0100 Trap_It:
0110 PRINT "Err=",ERR," line ",ERS
0120 EXIT ERR
->a=new("A")
->?a'divide(10)
 10
->?a'divide(7)
 14.29
->?a'divide(0)
Err= 40 line  60
Error #40: Divide check or numeric overflow
->
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Re: error handling in objects
« Reply #3 on: July 21, 2022, 01:25:26 PM »
Thanks Mike,
The goal is to not do an exit err or escape err which triggers the main error handler.  Just silently return messages so they can be logged unless an exit err or escape err is put into the object for critical tasks.  Perhaps I've over thought this and a generic return on the seterr procedure will work?