Post Reply 
How to check for and trap a CAS error in a program?
11-30-2018, 03:27 PM
Post: #1
How to check for and trap a CAS error in a program?
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?
Find all posts by this user
Quote this message in a reply
11-30-2018, 05:48 PM
Post: #2
RE: How to check for and trap a CAS error in a program?
You can trap CAS errors in CAS programs.
Find all posts by this user
Quote this message in a reply
12-01-2018, 02:01 AM
Post: #3
RE: How to check for and trap a CAS error in a program?
If your expected result will never be a string, testing for a string after could also work for you I think.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
12-01-2018, 02:48 PM
Post: #4
RE: How to check for and trap a CAS error in a program?
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.
Find all posts by this user
Quote this message in a reply
12-01-2018, 03:41 PM
Post: #5
RE: How to check for and trap a CAS error in a program?
(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() ...
Find all posts by this user
Quote this message in a reply
12-02-2018, 02:25 AM (This post was last modified: 12-02-2018 02:25 AM by Jacob Wall.)
Post: #6
RE: How to check for and trap a CAS error in a program?
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
12-02-2018, 11:28 AM
Post: #7
RE: How to check for and trap a CAS error in a program?
(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. :-)
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)