Marklogic如何在抛出异常后继续循环

时间:2018-08-22 16:01:48

标签: marklogic

我想知道在引发异常和文档总数失败后如何继续循环。

示例:使文档010291.xml失败,计数为4000,然后再次继续循环。

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
}

2 个答案:

答案 0 :(得分:2)

很可能有更好的方法来处理您要对该代码执行的任何操作,但是特别是为了在发生错误的情况下完成迭代,您需要在{{下应用try / catch 1}},在您希望引发异常的调用周围:

for

使用try / catch会有一些开销,因此您可能会注意到该查询的速度变慢,原因是对序列中的每个项目都调用了一次。

答案 1 :(得分:1)

将try-catch语句放入循环内

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
  }