使用LotusScript处理错误 - 继续执行程序

时间:2016-01-14 15:37:38

标签: lotus-notes lotusscript lotus

关于如何使用这个LotusScript片段继续执行程序,我有点迷失。它从视图中提取所有文档,但是,它正在命中包含“溢出”错误的某个文档,该错误会停止程序,而不是忽略它并继续下一个文档。正在打印出错误消息,因此很明显代码正在进入ErrorHandler,然后结束。

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

1 个答案:

答案 0 :(得分:4)

添加一行

On Error GoTo errHandler
在临之前

并在打印后用

替换行
Resume nextDoc

您的代码可能会导致无限循环。如果例如视图“Main”不可用,则行
Set view = db.getView("Main")会导致错误。执行将跳转到errHandler并从那里跳到nextDoc。文档Set doc = view.getNextDocument(doc)也会引发错误Nothing。执行将跳转到errHandler并从那里跳到nextDoc并且......我们有一个不定式循环。

您可以通过以下错误处理来避免这种情况:

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