Interrupt
RubyFATALCommonSystem

Ctrl+C pressed — keyboard interrupt

Quick Answer

Rescue Interrupt to print a newline or run cleanup, then re-raise or exit to avoid swallowing the signal.

What this means

Subclass of SignalException raised when SIGINT is received (usually Ctrl+C in a terminal). Rescuing it allows programs to perform cleanup before exiting.

Why it happens
  1. 1User presses Ctrl+C in an interactive terminal
  2. 2Process receives SIGINT from another process (e.g., test runner timeout)

Fix

Rescue Interrupt for clean terminal output

Rescue Interrupt for clean terminal output
begin
  run_long_task
rescue Interrupt
  puts "
Aborted by user"
  exit 1
end

Why this works

Rescuing Interrupt lets you print a clean message and perform any teardown before the process terminates.

Code examples
Graceful Ctrl+C handlerruby
trap('INT') { puts "
Stopping..."; exit }
loop { sleep 1 }
Rescue Interruptruby
begin
  server.start
rescue Interrupt
  server.stop
  puts 'Server stopped'
end
Re-raise after cleanupruby
rescue Interrupt
  cleanup_resources
  raise   # let the program actually terminate
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors