SystemExit
RubyINFOCriticalSystem

Program exit requested via exit or exit!

Quick Answer

Do not rescue SystemExit in application code; use at_exit hooks for cleanup instead.

What this means

Raised when exit or exit! is called to terminate the process. SystemExit carries the exit status code. It is a subclass of Exception, not StandardError, so it is not caught by bare rescue.

Why it happens
  1. 1Calling Kernel#exit or Kernel#exit! explicitly
  2. 2Calling abort which calls exit(1)

Fix

Use at_exit for cleanup instead of rescuing SystemExit

Use at_exit for cleanup instead of rescuing SystemExit
at_exit do
  DB.disconnect
  puts "Shutdown complete"
end

# Later in code:
exit 0   # at_exit hook runs before process terminates

Why this works

at_exit registers a block that runs on any exit, including SystemExit, without interfering with the exit flow.

Code examples
SystemExit raised by exitruby
begin
  exit 1
rescue SystemExit => e
  puts "Exiting with status #{e.status}"
  raise   # re-raise to let the process actually exit
end
exit! skips at_exit hooksruby
exit!   # terminates immediately, no at_exit, no ensure
Checking ancestryruby
SystemExit < Exception    # => true
SystemExit < StandardError # => false
Sources
Official documentation ↗

Ruby Core Documentation

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

← All Ruby errors