HP Forums
How to check for and trap a CAS error in a program? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: How to check for and trap a CAS error in a program? (/thread-11880.html)



How to check for and trap a CAS error in a program? - BruceH - 11-30-2018 03:27 PM

I have the following sample program in the emulator, latest ROM.[/quote]
PHP Code:
EXPORT FOO()
BEGIN
  LOCAL i
p;
  PRINT();
  
:= 0;
  
IFERR
    
WHILE DO
      
:= CAS.part("sin(45)"i);
      PRINT(
p);
      
:= 1;
    
END;
  
THEN
    
PRINT("Error at part "+i);
  ELSE
    PRINT(
"No error");
  
END;
  RETURN 
1;
END
When I run it I get the following on the terminal:
Quote:sin
45
part(sin(45),i_,i_)
Error: Bad Argument Value
No error

As you can see from the messages, the IFERR error handler didn't trap the CAS error. Is there another 'official' way to do this?


RE: How to check for and trap a CAS error in a program? - parisse - 11-30-2018 05:48 PM

You can trap CAS errors in CAS programs.


RE: How to check for and trap a CAS error in a program? - Tim Wessman - 12-01-2018 02:01 AM

If your expected result will never be a string, testing for a string after could also work for you I think.


RE: How to check for and trap a CAS error in a program? - BruceH - 12-01-2018 02:48 PM

Thanks Tim.

Actually it is the part() function that I want to trap and it can return a string legitimately.

After reading Parisse's suggestion, it occurred to me that rather than try and trap the error from part() in my main code, I could just use a separate #cas...#end bracketed function which calls part() with 1, 2, 3, .. etc until it fails and then return the max value to use as the loop upper bound in my main code with no worries about errors.


RE: How to check for and trap a CAS error in a program? - Albert Chan - 12-01-2018 03:41 PM

(12-01-2018 02:48 PM)BruceH Wrote:  separate #cas...#end bracketed function which calls part() with 1, 2, 3, .. etc until it fails and then return the max value

That look like the function len() ...


RE: How to check for and trap a CAS error in a program? - Jacob Wall - 12-02-2018 02:25 AM

One thing to remember, if you have a PPL program with an IFERR statement, there is an excellent chance that the next CAS command you use will crash your program.

Example:
Code:
EXPORT Demo_IFERR()
BEGIN
  LOCAL m;
  RECT_P();
  TEXTOUT_P("Press ON to generate an error ...",G0,5,5,4);
  REPEAT  
    IFERR m:=WAIT(-1); THEN
      m:=4;
      TEXTOUT_P("Error triggered ...",G0,5,25,4);
    ELSE
      TEXTOUT_P("Error avoided ...",G0,5,25,4);
    END;
  UNTIL TYPE(m)==0;
  TEXTOUT_P("Program still running ...",G0,5,45,4);
  WAIT(1);
  MSGBOX(stddev({0.01,0.02,-0.015,0.005}));
END;

You can force the error in the program above by pressing the ON key on the keyboard. If you do so, the crash/error happens when the "stddev" command is encountered. This is true for all CAS commands. Has caught me a few times now, and difficult to debug if you're not aware of this. I have written multiple substitute routines to get around the need for CAS commands as a result.


RE: How to check for and trap a CAS error in a program? - BruceH - 12-02-2018 11:28 AM

(12-01-2018 03:41 PM)Albert Chan Wrote:  
(12-01-2018 02:48 PM)BruceH Wrote:  separate #cas...#end bracketed function which calls part() with 1, 2, 3, .. etc until it fails and then return the max value

That look like the function len() ...
Thanks Albert - length() is indeed the function that I'm after. I had previously tried DIM but it doesn't work for all expressions. I'd also tried SIZE with no luck, but overlooked length.

And thanks also to Jacob for the warning - I had no idea I was going to open a can of worms. :-)