How to catch this error?

Posted by jamesmc on 19-Jun-2018 06:03

Hi,

I use the below code and I can successfully catch the error 98 that is raised (unable to open file)..

DEFINE VARIABLE lvMessage   AS CHARACTER   NO-UNDO.

DEFINE STREAM strFile.

MAINBLOCK:
DO ON ERROR UNDO, LEAVE:

    ASSIGN
        lvMessage = "".

    OUTPUT STREAM strFile TO VALUE( "c:\tempr\blah.txt" ).

    OUTPUT STREAM strFile CLOSE.

    CATCH lvError AS Progress.Lang.Error:

        ASSIGN
            lvMessage = SUBSTITUTE("Caught! &1", lvError:GetMessage(1) ).

    END CATCH.

END. /* MAINBLOCK */

MESSAGE lvMessage VIEW-AS ALERT-BOX INFO BUTTONS OK.

But when I change the code to the below I no longer get the message from the catch block, instead the program errors...

DEFINE TEMP-TABLE ttList NO-UNDO
    FIELD tDate AS DATE
    FIELD tSeq  AS INTEGER
    FIELD tText AS CHARACTER
INDEX tabix IS PRIMARY tDate tSeq.

DEFINE VARIABLE lvMessage   AS CHARACTER   NO-UNDO.

DEFINE STREAM strFile.

/************************************/
CREATE ttList.
ASSIGN ttList.tDate = TODAY
       ttList.tSeq  = 1
       ttList.tText = "A".

CREATE ttList.
ASSIGN ttList.tDate = TODAY
       ttList.tSeq  = 2
       ttList.tText = "B".

CREATE ttList.
ASSIGN ttList.tDate = TODAY
       ttList.tSeq  = 3
       ttList.tText = "C".

/************************************/
MAINBLOCK:
DO ON ERROR UNDO, LEAVE:

    ASSIGN
        lvMessage = "".

    FOR EACH ttList NO-LOCK BREAK BY ttList.tDate BY ttList.tSeq:

        IF FIRST-OF(ttList.tDate) THEN
        DO:
            OUTPUT STREAM strFile TO VALUE( SUBSTITUTE("c:\tempr\&1.txt", ttList.tSeq) ).
        END.

        PUT STREAM strFile UNFORMATTED
            ttList.tText
            SKIP.

        IF LAST-OF(ttList.tDate) THEN
        DO:
            OUTPUT STREAM strFile CLOSE.
        END.
    END.

    CATCH lvError AS Progress.Lang.Error:

        ASSIGN
            lvMessage = lvError:GetMessage(1).

    END CATCH.

END. /* MAINBLOCK */

MESSAGE lvMessage VIEW-AS ALERT-BOX INFO BUTTONS OK.

How is the catch not working in the second example?

Windows 7, OE 10.2B08

Thanks

Posted by Stefan Drissen on 19-Jun-2018 06:15

If you were on a higher version you could use:

BLOCK-LEVEL ON ERROR UNDO, THROW.

But since you are not, you will need to add explicit ON ERROR UNDO, THROW to the FOR EACH.

All Replies

Posted by Stefan Drissen on 19-Jun-2018 06:15

If you were on a higher version you could use:

BLOCK-LEVEL ON ERROR UNDO, THROW.

But since you are not, you will need to add explicit ON ERROR UNDO, THROW to the FOR EACH.

Posted by James Palmer on 19-Jun-2018 06:17

Your catch is not in the correct block for the error. This works:

DEFINE TEMP-TABLE ttList NO-UNDO
    FIELD tDate AS DATE
    FIELD tSeq  AS INTEGER
    FIELD tText AS CHARACTER
INDEX tabix IS PRIMARY tDate tSeq.
 
DEFINE VARIABLE lvMessage   AS CHARACTER   NO-UNDO.
 
DEFINE STREAM strFile.
 
/************************************/
CREATE ttList.
ASSIGN ttList.tDate = TODAY
       ttList.tSeq  = 1
       ttList.tText = "A".
 
CREATE ttList.
ASSIGN ttList.tDate = TODAY
       ttList.tSeq  = 2
       ttList.tText = "B".
 
CREATE ttList.
ASSIGN ttList.tDate = TODAY
       ttList.tSeq  = 3
       ttList.tText = "C".
 
/************************************/
MAINBLOCK:
DO ON ERROR UNDO, LEAVE:
 
    ASSIGN
        lvMessage = "".
 
    FOR EACH ttList NO-LOCK BREAK BY ttList.tDate BY ttList.tSeq:
 
        IF FIRST-OF(ttList.tDate) THEN
        DO:
            OUTPUT STREAM strFile TO VALUE( SUBSTITUTE("c:\tempr\&1.txt", ttList.tSeq) ).
        END.
 
        PUT STREAM strFile UNFORMATTED
            ttList.tText
            SKIP.
 
        IF LAST-OF(ttList.tDate) THEN
        DO:
            OUTPUT STREAM strFile CLOSE.
        END.
        CATCH lvError AS Progress.Lang.Error:
     
            ASSIGN
                lvMessage = lvError:GetMessage(1).
     
        END CATCH.
    END.
 
 
END. /* MAINBLOCK */
 
MESSAGE lvMessage VIEW-AS ALERT-BOX INFO BUTTONS OK.

Posted by James Palmer on 19-Jun-2018 06:18

Or as Stefan says.

Posted by jamesmc on 19-Jun-2018 06:31

Thanks guys.  I will go with Stefan's suggestion as the code is a little more detailed than the example I provided and there is more that could error so scoping both blocks to the one catch works better for me

This thread is closed