I already commented about Expect elsewhere in this thread, so I should probably pipe down, but thought it might be worth it here as well because Expect has been handling these kinds of exceptions and control transfers/flows with the full power of a robust programming language for decades. You might have a look at it for ideas and inspiration.
https://en.wikipedia.org/wiki/Expect