title: Avoiding accidentally catching KeyboardInterrupt and SystemExit in Python 2.4
body: <p>In Python scripts, there are many cases where a keyboard interrupt (Ctrl-C) fails to kill the process because of a bare <code>except</code> clause somewhere in the code:</p>
  
  <pre><code>try:
      foo()
  except:
      bar()
  </code></pre>
  
  <p>The standard solution in Python 2.5 or higher is to catch <code>Exception</code> rather than using bare <code>except</code> clauses:</p>
  
  <pre><code>try:
      foo()
  except Exception:
      bar()
  </code></pre>
  
  <p>This works because, as of Python 2.5, <code>KeyboardInterrupt</code> and <code>SystemExit</code> inherit from <code>BaseException</code>, not <code>Exception</code>. However, some installations are still running Python 2.4. How can this problem be handled in versions prior to Python 2.5?</p>
  
  <p>(I'm going to answer this question myself, but putting it here so people searching for it can find a solution.)</p>