Marklogic How to continue looping after throw catching exception

梦想与她 提交于 2019-12-11 17:24:25

问题


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
}

回答1:


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
  }



回答2:


It is very likely that there is a better way to do whatever you're trying to do with that code, but specifically to complete the iterations in case of an error, you need to apply the try/catch below the for, around the call that you expect to throw an exception:

let $uris := 
  cts:uris((),'limit=200000',
    cts:and-query(
     cts:collection-query("/TRA")
    ))
for $uri in $uris
let $result :=
  try { fn:exists(doc($uri)) }
  catch($err) { $err }
return
  typeswitch($result)
  case element(error:error) return ("received the following exception: ", $result)
  default return $result
, 
xdmp:elapsed-time()

There is some overhead to using try/catch, so you may notice this query slow down as a result of calling it once for every item in the sequence.



来源:https://stackoverflow.com/questions/51970860/marklogic-how-to-continue-looping-after-throw-catching-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!