title: Capturing Ctrl-c in ruby
body: <p>I was passed a long running legacy ruby program, which has numerous occurrences of </p>
  
  <pre><code>begin
    #dosomething
  rescue Exception =&gt; e
    #halt the exception's progress
  end
  </code></pre>
  
  <p>throughout it. </p>
  
  <p>Without tracking down every single possible exception these each could be handling (at least not immediately), I'd still like to be able to shut it down at times with <kbd>Ctrl</kbd><kbd>C</kbd>.</p>
  
  <p>And I'd like to do so in a way which only adds to the code (so I don't affect the existing behavior, or miss an otherwise caught exception in the middle of a run.)</p>
  
  <p>[<kbd>Ctrl</kbd><kbd>C</kbd> is SIGINT, or SystemExit, which appears to be equivalent to <code>SignalException.new("INT")</code> in Ruby's exception handling system. <code>class SignalException &lt; Exception</code>, which is why this problem comes up.]</p>
  
  <p>The code I would like to have written would be:</p>
  
  <pre><code>begin
    #dosomething
  rescue SignalException =&gt; e
    raise e
  rescue Exception =&gt; e
    #halt the exception's progress
  end
  </code></pre>
  
  <p><strong>EDIT: This code works, as long as you get the class of the exception you want to trap correct. That's either SystemExit, Interrupt, or IRB::Abort as below.</strong> </p>