Search code examples
marklogic

Marklogic How to continue looping after throw catching exception


I would like to know how to continue looping after throwing exception and document fail at total count.

example: fail document 010291.xml at count 4000 and continue loop again.

xquery version "1.0-ml";
try {
  let $uris := cts:uris((),(),
                 cts:and-query(
                   cts:collection-query("/TRA")
                 )
  )[1 to 200000]

  for $uri in $uris
  return    
    if (fn:exists(doc($uri))) then ()
    else $uri,

  xdmp:elapsed-time()
} catch($err) { 
  "received the following exception: ", $err
}

Solution

  • Put the try-catch statement inside the loop

    xquery version "1.0-ml";
    
    let $uris := cts:uris((),(),
                   cts:and-query(
                     cts:collection-query("/TRA")
                   )
    )[1 to 200000]
    
    for $uri in $uris
    return
      try{(
            if (fn:exists(doc($uri))) 
            then ()   
            else $uri,
            xdmp:elapsed-time()
          )
      } catch($err) { 
        "received the following exception: ", $err
      }