How do I go to next document on error

I have been a developer for several years now and shamefully admit I have never really incorporated error handling – I know, bad.

I am now writing a script that grabs the first document in a view, scans it for info, copies it if it meets criteria, and then goes to the next document. I need to incorporate some error handling so that if there is an error in scanning or copying the document, the agent will continue on to the next document and not just stop. Below is a snippet from my code, could someone tell me what I need to add to make this happen?

Set doc = v.GetFirstDocument()

While Not(doc Is Nothing)

sourceto = doc.SendTo(0)

If Instr(sourceto, “sender”) = 0 Then

Call doc.CopyToDatabase(copydb)

Print "Copied one document from " + db.Title

End If

Set doc = v.GetNextDocument(doc)

Wend

Subject: How do I go to next document on error

Nin Hao Jennifer,

You could structure your code into subroutines and add a noteslog as error destination.

'Please for heavens sake:

OPTION DECLARE

Sub Initialize

Dim db as Notes DataBase

Dim doc as NotesDocument

Dim v as NotesView

Dim nlog as NotesLog



'Code starts here

On Error Goto Err_MyAgent

Dim s as New NotesSession

Set db = s.currentDatabase

Set nlog = new NotesLog("My Agent")

nlog.logErrors = true

Call nlog.OpenMaillog("my email',"Results of MyAgent") 'Typically this would be a mail-in databas



Set v = db.Getview("YourViewName')

Set doc = v.GetFirstDocument()

Do until doc Is Nothing

     Call WorkOnThisDoc(doc, nlog)

Set doc = v.GetNextDocument(doc)

End Do

Exit_MyAgent:

  On Error Resume Next

  nlolg.close

  Exit sub

Err_MyAgent:

 if not nlog is nothing then

      call nlog.logerror(err,error$+" in MyAgent")

 end if

 Resume Exit_MyAgent 'Make it a rule --- if code is in exit

End Sub

Sub WorkOnThisDoc(doc as NotesDocument, nlog as NotesLog)

Dim sourceto as String

On Error Goto Err_WorkOnThisDoc

if not doc.hasitem(“SendTo”) then

goto Exit_WorkOnThisDoc 'Goto is acceptable because it shortens the if statement

end if

sourceto = doc.SendTo(0)

If Instr(sourceto, “sender”) = 0 Then

 Call doc.CopyToDatabase(copydb)

 Print "Copied one document from " + db.Title

 Call Nlog.logaction("Copied one document from " + db.Title)

End If

Exit_WorkOnThisDoc:

  Exit Sub

Err_WorkOnThisDoc:

call nlog.logerror(err,Error$+" in WorkOnThisDoc")

Resume Exit_WorkOnThisDoc

End Sub

The code might contain typos since I just typed it of memory.

Important concepts here:

Option declare

make a loop short – just call a subroutine/function inside

Use Do instead of While

Use a NotesLog for error logging

Use a consitent schem for error handling: for the initilize the labels are the agent name with Err_ and Exit_ in front, for subs/functions the sub/function name

Hope that helps

:slight_smile: stw

Subject: How do I go to next document on error

You don’t necessarily need error handling for this case, some obligatory value checking should be much cleaner and faster here:

If db.IsOpen Then

If copydb.IsOpen Then

If not v Is Nothing Then

Set doc = v.GetFirstDocument()

While Not(doc Is Nothing)

sourceto = doc.SendTo(0)

If Instr(sourceto, “sender”) = 0 Then

Set doc2 = doc.CopyToDatabase(copydb)

If Not doc2 Is Nothing Then

 Print "Copied one document from " + db.Title

Else

 MsgBox "Error in copying doc."

End If

End If

Set doc = v.GetNextDocument(doc)

Wend

Else

MsgBox “View v is not accessible.”

End If

Else

MsgBox “Database copydb has not been opened yet.”

End If

Else

MsgBox “Database db has not been opened yet.”

End If

Once those basic checks are done, you can still have an error checking, in case of errors which would normally break the execution. The easiest way is to put at the beginning:

Sub Initialize

On Error Goto Hell

'your normal code here

Exit Sub

Hell:

Resume Next

Exit Sub

End Sub

This would catch all possible errors, but you can also add error handling for specific errors only, you need to find their error numbers, for example by printing the Err variable when such an error occurs.

Oh btw, my “Hell” label is just a shortcut for “Handle errors linear logic”, but it makes the code much easier understandable :slight_smile:

Subject: RE: How do I go to next document on error

Thanks, the “Goto Hell” was exactly what I needed and it worked like a charm. I changed the wording a bit, but I appreicate your style. :slight_smile: