Is there any way to defer an exception coming from an external source for the duration of a certain block of code? For example:
some_statement_one
some_statement_two
# Begin suppression of KeyboardInterrupt
with KeyboardInterrupt suppressing context:
important_statement_one
important_statement_two
important_statement_three
some_statement_three
What I would like to be able to do is to delay reception of a KeyboardInterrupt
(or whatever error(s) I specify) until after then end of the important block. That is, if during the execution of important_statement_one
the user sends a keyboard interrupt, the code will continue normally until after it has finished important_statement_three
. Then, the KeyboardInterrupt
will be received as if it had been sent right then.
My concern is only with an exception that is generated external to the code being executed, such as KeyboardInterrupts, SystemExits, etc. Is there even a meaningful way to distinguish between an exception raised by code being executed and an exception raised due to something external? I probably can't do what I'm trying to do unless there is.
Some additional information: I'm using the technique suggested in Is there any way to kill a Thread in Python? to send exceptions to threads. Does raising an exception like this make a difference?
I can't really use try/except blocks for this - I'm some information between threads, so if the exception is sent on the line where the information is passed then the information will be lost.