```class Foo { val seq: Sequence<Bar> = ...
# getting-started
y
Copy code
class Foo {
    val seq: Sequence<Bar> = //...
    fun process() {
        for (elem in seq) {
            // do something with elem, 
            // but maybe throw an exception
        }
    }
}
is it sound to call
process()
again if an exception is thrown? will the sequence continue from the element immediately after the element for which the exception was thrown?
c
Yes. As long as the exception wasn't thrown by the sequence itself, it is in valid state even if an exception appeared somewhere else (and it will continue from the next element which wasn't yet read).
The element read at the time the exception happened is lost though (it was removed from the sequence by the for loop), so you have to recover it yourself.
y
thanks!
c
If your goal is to communicate to the caller whether they should retry the operation or not, there is probably a better way to do it, though. What's your objective?
y
I had the (probably bad) idea of wrapping that
process()
function in a loop that keeps retrying the function until
seq
is exhausted. the
catch
block of the wrapping function does something with the caught exceptions.
now that I think about it, if I do end up doing this, the iteration should be in the wrapping function, and
process
should take the next yielded value
…which basically is an event loop