问题
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