Error Handling for Query-Open

Posted by qcace on 19-Apr-2010 15:01

Hello,

Is there a better way to return an OPEN-QUERY error ???

Not sure is what I have below is the best way.  This code is running on an app server.

Without the IF error-status statement, if there is a problem with the vQuery it will continually post error log messages into our log file.

With the IF error-status statement, it randomly returns an error even though there is no problem with the query.

Thanks.

...

CREATE QUERY hdlRadioQuery.

hdlRadioQuery:

SET-BUFFERS(BUFFER cl3gComm:HANDLE, BUFFER cl3gDevice:HANDLE).

hdlRadioQuery:

QUERY-PREPARE(vQuery).

hdlRadioQuery:

QUERY-OPEN.

IF ERROR-STATUS:ERROR THEN DO:

   RETURN ERROR 'Error opening query.'.

END.

...

All Replies

Posted by rbf on 19-Apr-2010 15:29

Append NO-ERROR to your statements. You need to do that anyway since ERROR-STATUS returns the status of your last NO-ERROR. Since you don't have any you are getting unexpected results in this code. In addition you have error conditions that are not caught.

If you are on 10.1C+ you could also use structured error handling (CATCH).

Posted by sarahm on 19-Apr-2010 15:29

ERROR-STATUS is only set for the last statement that was called with NO-ERROR.  You don't have any NO-ERROR statements in your sample, so I imagine the the reason it appears random is because prior code has statements with NO-ERROR that may or may not be setting ERROR-STATUS:ERROR to true.  A built-in method such as QUERY-OPEN will not set ERROR-STATUS:ERROR to true, but it will set messages, so you need to use the trick of checking NUM-MESSAGES instead of ERROR.

Of course you could use structured error handling and write a CATCH block to handle your error condition too.

Posted by qcace on 19-Apr-2010 16:11

Works great now!

Thanks.

Posted by olivier.dunemann on 21-Apr-2010 05:41

Alternately, you can also code something like:

DEF VAR lOK AS LOG NO-UNDO.

lOK = hdlRadioQuery:QUERY-PREPARE(vQuery) NO-ERROR.

IF NOT lOK THEN...

or

IF ERROR-STATUS:ERROR THEN...

In other words (as mentioned in the "OpenEdge Reference Help" on QUERY-PREPARE() method):

If the QUERY-PREPARE method encounters an  error, it returns FALSE and generates an error message, but does not raise  ERROR. If you use the QUERY-PREPARE method in a statement that uses the NO-ERROR  option and an error occurs, the QUERY-PREPARE method returns FALSE and diverts  the error to the ERROR-STATUS system handle. In  either instance, ERROR-STATUS:ERROR returns FALSE. You can get information on  the error through the GET-MESSAGE method of the ERROR-STATUS system handle, as  usual.
Posted by rbf on 21-Apr-2010 11:27

You are absolutely right.

This thread is closed