title: Can external exceptions in Python be deferred for the duration of a code block?
body: <p><strong>Is there any way to defer an exception coming from an external source for the duration of a certain block of code?</strong> For example:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What I would like to be able to do is to delay reception of a <code>KeyboardInterrupt</code> (or whatever error(s) I specify) until after then end of the important block. That is, if during the execution of <code>important_statement_one</code> the user sends a keyboard interrupt, the code will continue normally until after it has finished <code>important_statement_three</code>. Then, the <code>KeyboardInterrupt</code> will be received as if it had been sent right then.</p>
  
  <p>My concern is only with an exception that is generated external to the code being executed, such as KeyboardInterrupts, SystemExits, etc. <strong>Is there even a meaningful way to distinguish between an exception raised by code being executed and an exception raised due to something external?</strong> I probably can't do what I'm trying to do unless there is.</p>
  
  <p>Some additional information:
  I'm using the technique suggested in <a href="https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python">Is there any way to kill a Thread in Python?</a> to send exceptions to threads. Does raising an exception like this make a difference?</p>
  
  <p>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.</p>