Error-status:error

dschuchman

New Member
would like to know how to catch run-time errors.
have the following code(error.p) which exceeds array bounds:
/* error.p */
DEFINE VARIABLE v-array AS INTEGER EXTENT 10.
DEFINE VARIABLE v-i AS INTEGER.
DO v-i = 1 TO 11 ON ERROR UNDO, LEAVE:
v-array[v-i] = v-i.
END.

i go into editor and run the following:
RUN VALUE("error.p") NO-ERROR.
IF ERROR-STATUS:ERROR THEN DO:
DISPLAY "Have error".
END.

the "Have error" does not show up. any ideas how i can catch run-time errors like this occuring?
 

parul

Member
Error-status:error is not the most reliable way to catch errors.
Use error-status:num-messages > 0.

-Parul.
 
Num-messages or _msg(1) will be set if the error check is reached.

Also, wrap your run in a DO ON STOP, as the STOP condition is raised when the program is not found.

You may also like to test before running with the SEARCH function.
 
eg.
Code:
DO ON STOP UNDO, LEAVE :

       RUN VALUE("error.p") NO-ERROR.
 
END.

MESSAGE _msg(1) SKIP ERROR-STATUS:NUM-MESSAGES SKIP ERROR-STATUS:ERROR
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

ERROR-STATUS:NUM-MESSAGES is the correct test though, as Parul says.
 

Casper

ProgressTalk.com Moderator
Staff member
Or you could put in a retry block:

DEFINE VARIABLE v-array AS INTEGER EXTENT 10.
DEFINE VARIABLE v-i AS INTEGER.

DO v-i = 1 TO 11 ON ERROR UNDO, RETRY:
IF RETRY
THEN RETURN ERROR 'something went wrong'.

ASSIGN v-array[v-i] = v-i NO-ERROR.
IF ERROR-STATUS:ERROR
THEN UNDO, RETRY.
END.

then this code works:

Code:
RUN error.p NO-ERROR.
IF ERROR-STATUS:ERROR
THEN DISPLAY "Have error" RETURN-VALUE.

Casper.
 

WillieG

New Member
The best strategy would be to avoid errors rather than to catch them.
In your sample code I would replace the 11 with EXTENT(v-array), in this way you can never exceed the array bound.
Your error.p would then look like this:
/* error.p */
DEFINE VARIABLE v-array AS INTEGER EXTENT 10.
DEFINE VARIABLE v-i AS INTEGER.
DO v-i = 1 TO EXTENT(v-array) ON ERROR UNDO, LEAVE:
v-array[v-i] = v-i.
END.
 

Casper

ProgressTalk.com Moderator
Staff member
I think that it is obvious that the example is made for the purpose of making an error to illustrate what would be a good way to catch it.

casper.
 
Top