The HP48 Homepage  

News My Programs Software Programming Articles Links Sitemap Search Feedback    

Programming in User RPL - Error handling

The HP48 user programs are safe. Error handling is done automatically, the programer doesn't have to worry about that. And, if some error condition occours, for example if there aren't enough arguments, then the only thing that will happen is that a error beep and message will be produced, and the program will be stopped.

The fact that errors are checked, however, doesn't mean that you don't have to worry about that. Sometimes, you really don't have. But there are times that it is necessary. If a program, for example needs a positive real number, but the user enters a negative, probably no error will occur in the operations of the program, but the resul will be wrong, or something like that. Because of this, there are some structures to handle errors.

The first of them is IFERR...THEN...ELSE...END. It's sintaxis is:

   detection clause
   clause if error
   normal clause

The detection clause is first evaluated. If an error occours, then clause if error is executed. Otherwise, normal clause is evaluated. ELSE normal clause may be omitted if there is no action to be taken only if there was no error.

Tip: Stopping loops
It isn't written on the manual, but you can exit a loop (START, DO and similars): Include the loop in an IFERR structure. Then, when you want to stop the loop, produce an error, like divide something by zero. Example: this loop won't write all the numbers:
<< IFERR 1 9 FOR i i IF i 5 > THEN
0 DUP / END NEXT THEN END "Did you see?" >>

But what if the program muts take different actions based on the kind of error that occured. Suppose the program should do something if there was a undefined result and another action if there was an infinite result. How can this be done? With ERRN, ERRM and ERR0. ERRN returns the number (in base format) of the last error. You can find a table of error values and messages on Appendix B of the manual. ERRM returns a string with the error message. And ERR0 clears the last error.

And the last command for error is DOERR. As the name says, it is used to produce an error. It accepts several arguments: the number of an error, in real or base format, a specified message to show, or 0. If you give the number of the error, the system error will be produced. If you give a string, the string will be shown on the status area, and in both cases the program will be stopped. If you give 0, the program will simply be stopped.

Tip: Another way to stop a program execution
0 DOERR can stop a program while it is running. But there is another way, smaller and faster. Simply insert KILL where you want the program to be stopped.

Previous Page Next Page

This page was created by Eduardo M Kalinowski