Discussion
The mystery here is the finally block’s missing output. Where did it go?
No matter the reason, when a try block exits, the corresponding finally block always runs—right? That’s its job, as we learned in Puzzle 14, Just One More Thing.
But it turns out the suspension-point powered sequence builder has a surprising ability to undermine that critical control-flow guarantee. When we interrupt our number sequence early, the generator code inside just stops in its tracks, and the finally block never runs.
Failing to run finally blocks in a real app can cause bugs and hurt performance by leaving resources unclosed and data in an inconsistent state—so this could be a big problem!
Suspending Sequences
A Sequence in Kotlin produces its values ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access