Search code examples
lotus-noteslotusscriptlotus

Error Handling With LotusScript - Continue Program Execution


I'm a little lost as to how to continue the program execution with this LotusScript snippet. It extracts all the documents from a view, however, it is hitting a certain document that contains an 'Overflow' error which stops the program, rather than ignoring this and continuing to the next document. The error message is being printed out, so it's clear that the code is entering the ErrorHandler, and then subsequently ends.

Option Public
Option Declare

Sub Initialize
    'init stuff, etc

    Set view = db.getView("Main")
    Set doc = view.getFirstDocument()
    Set lastDoc = view.getLastDocument()
    k = 0

    While (Not doc is Nothing)
        dealId = doc.DealId(0)
        If(doc.HasEmbedded) Then 
            Set body = doc.GetFirstItem("Body")         
            If(Not body Is Nothing) Then 
                'code to extract all attachments on a document
            End If
        End If

nextDoc:
        Set doc = view.getNextDocument(doc)
        k = k + 1
    Wend    
    Exit Sub 

errHandler:
    Print "Get error when process document with dealId=" & dealId & " at line " & CStr(Erl) & ". Err=" & CStr(Err) & ", error=" & Error
    GoTo nextDoc
    'this should continue execution of nextDoc
End Sub

Solution

  • Add a line

    On Error GoTo errHandler
    

    before While and replace line after Print with

    Resume nextDoc
    

    Your code might cause an infinite loop though. If for example view "Main" is not available, the line
    Set view = db.getView("Main") would cause an error. Execution would jump to errHandler and from there to nextDoc. The line Set doc = view.getNextDocument(doc) would throw an error too as doc is Nothing. Execution would jump to errHandler and from there to nextDoc and... we have an infinitive loop.

    You can avoid this with an error handling like this:

    nextDoc:
            Set doc = view.getNextDocument(doc)
            k = k + 1
        Wend    
    
    finito:
        Exit Sub 
    
    errHandler:
        If doc is Nothing then
            Print "This is a serious error before while..."
            Resume finito
        Else
            Print "Get error when process document with dealId=..."
            Resume nextDoc
        End If
    End Sub