WHEN ERROR in Qlib

Anything QL Software or Programming Related.
Post Reply
Tinyfpga
Gold Card
Posts: 252
Joined: Thu Sep 27, 2018 1:59 am

WHEN ERROR in Qlib

Post by Tinyfpga »

I have been struggling recently with a program I am trying to write using the instructions EVAL and COMPIL$.

My problem lies with the fact that this combination within QPC2 and Qlib fails when a non arithmetic string is passed to it.

Now you may ask why one would to do this and the answer to that, is that one wouldn't, but one could enter an invalid sting via
a typing error.

I have tried two ways to solve this problem and neither have been successful.

The first was to parse the entry string and eliminate erroneous entries. This has proven to be quite difficult to do.

The second was to use the sequence
WHEN ERROR
point out that an invalid string has been entered.
END WHEN.

Inserting the above into my program causes Qlib to stop with the errors Ambiguous name on WHEN ERROR and END WHEN.

Has anyone used WHEN ERROR in Qlib or does anyone know what I might be doing wrong?


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: WHEN ERROR in Qlib

Post by dilwyn »

I'm not familiar with using the extensions you mention and don't know if they can work in compiled programs or not. If the extensions you mention work in interpreted BASIC but not when compiled, it implies they cannot be compiled or used from compiled programs.

WHEN ERRor doesn't work on some QL ROMs such as AH or JM anyway, and even on more recent versions of SuperBASIC on a QDOS system can go wrong under some circumstances. The implementation of WHEN ERRor in QLiberator also varies. Some versions don't support it at all, more recent versions do to some extent. Always helps if you can give versions when reporting things like this. Section 11.1 of the original QLib manual states that "For these reasons this form of error trapping is not supported by QLiberator"

I've tried this just now on QPC2 v5.02 using QLib v3.45a and it works on a simple level. Here's an example I used:

Code: Select all

10 WHEN ERRor
20   PRINT\'BANG...Something went wrong @ line ';ERLIN
30   RETRY
40 END WHEN
50 CLS
60 REPeat loop
70   INPUT'Enter a value ';ip
80   IF ip=27 THEN EXIT loop : REM entering 27, code of ESC, lets you quit.
90 END REPeat loop
100 WHEN ERRor
110   CONTINUE
120 END WHEN
Try various 'errors' such as pressing ENTER to make a blank entry, enter letters or symbols, two full stops or anything else likely to cause an error. On QPC2 in SBASIC and compiled with QLib 3.45a it catches and reports the error. However, this somewhat misses the point with QLiberator, which has its own error trapping system which is probably more appropriate for this. Look up the Q_ERR_ON/Q_ERR_OFF keywords.

Code: Select all

10 CLS
20 REPeat loop
30   Q_ERR_ON 'input'
40   INPUT'Enter a value ';ip
50   fer = Q_ERR : Q_ERR_OFF 'input'
60   IF Q_ERR <> 0 THEN PRINT'OOPS!':NEXT loop
70   IF ip=27 THEN EXIT loop
80 END REPeat loop
Note that a WHEN ERRor with CONTINUE can be used to turn off this type of error trapping.

On a simpler level, a simple way to catch user input errors is to use a structure like this which catches most errors (though not something like entering a double dot):

INPUT num$
number = '0' & num$

Obviously, if you are entering complex values, equations etc to evaluate, the use of Q_ERR_ON and Q_ERR_OFF to catch errors is going to be the best solution for procedures. Q_ERR_ON works on procedures (machine code) by turning them into functions after a fashion - functions return a value, in this case the value returns in the function Q_ERR which can be used to trap errors. Meaning that pretty much anything which is a machine code procedure can be trapped.


Tinyfpga
Gold Card
Posts: 252
Joined: Thu Sep 27, 2018 1:59 am

Re: WHEN ERROR in Qlib

Post by Tinyfpga »

Thank you for your comprehensive reply. I will try to follow your examples in the environment you mention once I have created it.

I tried Q_ERR_ON and on my setup this did not seem to work with EVAL.


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: WHEN ERROR in Qlib

Post by dilwyn »

Most likely because it's a function, not a procedure (Q_ERR_ON only works with procedures).

Looking at the docs for EVAL, it looks like any errors are returned via the optional first parameter, which must be an integer variable:

result = EVAL([error%,]Compiled$,x[,y[,z]])

So, I guess that if the value of error% is negative on return from EVAL, it means there was an error of some kind. The square brackets indicate optional parameters. For the error returns to work, you should have an integer variable as first parameter, e.g.

expression$ = "1+2"
Compiled$ = COMPIL$(expression$)
result = EVAL(error%,Compiled$,x,y,z)
IF error% < 0 THEN REPORT error%
(untested!)

It says that "this provides an easy way to trap errors with QLiberated programs" so it looks like it's possible to use it in QLib'ed programs.

No indication in the docs as to whether COMPIL$ does error trapping.


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: WHEN ERROR in Qlib

Post by dilwyn »

I've tinkered a little (run out of time to take it further!) and come up with this which shows how to use WHEN ERRor on QPC2 at least (not tried on JS or MG or Minerva QDOS). Sadly, it doesn't work when compiled, although it runs fine n SBASIC. QLiberator compiles it without error, but when run, it freezes at the CONTINUE statement to resume program operation after an error. As I say, I have run out of time to take this further tonight, and as I don't normally use WHEN ERROR in compiled programs, I may be doing something wrong.

It asks you to enter an expression string - enter something like 2+2 to test the evaluation, then enter some nonsense text in the INPUT at line 230 to trigger an error. The WHEN ERROR statement beeps and prints an error message then issues a CONTINUE statement to resume after the point at which the error occurred. The variable "error_occurred" becomes 1 if the WHEN ERROR caught an error situation. This is used in lines 280 to 360 to decide whether the actions in that IF...END IF clause needs to be executed or to go back and ask for INPUT again.

No doubt it can be simplified and improved. I'd be interested to know why it locks up at the CONTINUE statement when compiled though. Something to be looked into.

For anyone who wants to tinker, the EVAL/COMPIL$ extensions are on the toolkits page on my site: https://dilwyn.qlforum.co.uk/tk/eval.zip

Code: Select all

100 REMark use WHEN ERRor to trap COMPIL$ errors
110 :
120 WHEN ERRor
130   BEEP 5000,50
140   error_occurred = 1 : REMark error flag
150   REPORT #1,ERNUM
160   PRINT
170   CONTINUE
180 END WHEN
190 :
200 CLS
210 REPeat loop
220   error_occurred = 0 : REMark reset error flag
230   INPUT"Expression to evaluate > ";expression$
240   IF expression$ = '' THEN EXIT loop
250   c$ = COMPIL$(expression$)
260   :
270   REMark did an error occur?
280   IF error_occurred = 0 THEN
290     :
300     x=0
310     error% = 0
320     result = EVAL(error%,c$,x)
330     PRINT"result (expression value)=";result
340     PRINT"EVAL error%=";error%
350     PRINT
360   END IF
370 END REPeat loop
380 STOP
390 :


Tinyfpga
Gold Card
Posts: 252
Joined: Thu Sep 27, 2018 1:59 am

Re: WHEN ERROR in Qlib

Post by Tinyfpga »

Dilwyn wrote :- No indication in the docs as to whether COMPIL$ does error trapping.
Your replies have been very helpful. Thank you for the time you have spent doing this.
I noticed an earlier forum discussion on EVAL type instructions and I am goibg to try and follow it when I have the time to do so.

I tested COMPIL$ on its own and it does not trap errors. This is where my problem lies. As you say COMPIL$ is a function so I can't use
Q_ERR_ON. That leaves WHEN ERRor as a possible solution to my problem

As you can see from my image I get the Qlib error "Ambiguous name" when I try to compile your sample code. I updated
my QPC2 environment to be the same as yours.
If WHEN ERRor works on your setup I must be doing something wrong.
One possibility is that I use QD and the original SMS2 Parser to write my programs and this might be doing something odd.

I am going to try and use the SMSQE parser (when I can find it) and see if that makes a difference. I have used it in the past
but did not like it for some reason.

It's the first time I have needed to error trap one of my programs in this way. If I can't use WHEN ERRor I will have another go
at trapping input errors before COMPIL$ing them. Clearly it would be better if COMPIL$ returned 0 on a nonsense input.

I am in the process of converting programs I wrote for Windows using Visual Basic. And the COMPIL$ problem reminds me of a similar problem with VB's FIND instruction which causes an interpreter error when it can't find an instance in a string instead of returning 0. Luckily the VB instruction ISERROR came to my rescue.

error1.JPG


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: WHEN ERROR in Qlib

Post by dilwyn »

I'm at a craft fair this weekend but do have my laptop with me. I'm not entirely sure what is happening with compiling WHEN ERRor - I use QLib 3.45a like you on QPC2 v5.02. If I get a chance this weekend I'll look into it and get back to. The "ambiguous name" error sounds like something else is loaded which clashes with those names. I'm aware that QLib 3.45a is split into a few different toolkit extension groups now. I LRESPR the QLIB_sys in my boot file for using QLiberator - is that what you do as well (in case different ways of setting up QLiberator have a bearing)?


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: WHEN ERROR in Qlib

Post by dilwyn »

I've had a quick look as it's quiet here and have attached a couple of files for you to try. My main worry is the rather flaky internet connection here (hotel wifi poor, so am tethering phone to laptop to try).

I've cut the WHEN ERRor example down to minimum, just trapping a numeric INPUT statement, to see if it'll compile. It did. So the zip attached has the _sav and _obj files for you to see if you can EXEC my compiled example (test_obj) and compile from my test_sav file. As you can see from the screen dump, it compiles OK on my system (QPC2 v5.02 and QLiberator 3.45a)
WHEN ERRor test compile
WHEN ERRor test compile
This is the test program - test_sav (or it might zip as test.sav) in the zip attached.

Code: Select all

100 WHEN ERRor
110   PRINT : REPORT #1,ERNUM
120   CONTINUE
130 END WHEN
140 :
150 CLS
160 REPeat loop
170   INPUT'Enter a number (0 to quit) > ';number
180   IF number = 0 THEN EXIT loop
190 END REPeat loop
Attachments
test.zip
WHEN error test files
(9.06 KiB) Downloaded 32 times


Tinyfpga
Gold Card
Posts: 252
Joined: Thu Sep 27, 2018 1:59 am

Re: WHEN ERROR in Qlib

Post by Tinyfpga »

I have just had thought. The TK2 extensions I am using are old. I have never bothered to check for a newer version because I have
never encountered a problem like this before. I am on your site at moment reviewing the TK2 section. I am assuming that WHEN ERRor
is a TK2 instruction.
I hope your are enjoying the craft fair and I thank you again for helping me. Some good may come of my attempts to write application
programs for SMSQE. It is taking time because I am still trying to fully use the PE programming environment without using Easyptr.
Does SMSQE already contain TK2 instructions?


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: WHEN ERROR in Qlib

Post by dilwyn »

If you are using QPC2 (or any SMSQ/E) it's best not to load a Toolkit 2 as all the extensions are already in QPC2 and SMSQ/E. That *may* explain why you're getting the ambiguous name problem. SBASIC on QPC2 already includes WHEN ERRor.


Post Reply