How to handle cancel during long editDocument()?

So when a long editDocument() is proceeding, and the user clicks the Stop button, what happens exactly, and how could we detect this instead of just crashing?

All I see in my plugin is the edits made to the scenegraph start failing out of the blue with exceptions.

This seems like a very poor UX.

So how could a plugin detect a cancellation and do something intelligent?