Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use exit to end a Ruby program normally, abort to report a failure with a message, and exit! only when you deliberately need to bypass exit handlers. These choices affect the process status seen by a shell and whether Ruby runs cleanup code.

How Ruby exit methods differ

A process exit status communicates whether a command succeeded. Conventionally, status 0 means success and a nonzero status means failure. Jesse Storimer describes exit codes as values from 0 through 255 in his Ruby process-termination guide, published in 2012 and updated in 2024.

Method or event When to use it Status What appears on STDERR Runs at_exit handlers?
exit Normal explicit termination or a deliberate result 0 by default; accepts a status code No automatic message Yes
abort Report a failure with context Failure status, normally 1 Prints the supplied message Yes, under normal termination behavior
exit! Immediate termination when handlers must not run Accepts a status No required message No
Unhandled exception Unexpected failure 1 Prints exception details Termination follows runtime behavior

Use exit to return a status code

Kernel.exit is Ruby’s normal explicit way to end a process. Calling exit with no argument returns success status 0; pass a nonzero integer when the program should signal failure. The shell can use that result to decide what to do next.

For example, a command that searches a file can exit with 0 when it finds a matching line and 1 when it does not. Then shell operators such as && and || can branch on the result: search_command && echo "found" runs the message only after success, while search_command || echo "not found" runs it after failure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall

Use abort when failure needs an explanation

abort "No matches found" reports the reason to STDERR and exits with a failure status. It is useful when a caller needs both a machine-readable failure result and a message a person can read. By contrast, exit 1 signals failure without automatically printing an explanation.

Run cleanup with at_exit

Register an at_exit block for work that should happen as Ruby terminates normally, such as removing a temporary file or closing a connection. Ruby runs registered exit handlers before normal termination; the Programming Ruby Kernel reference also describes object finalizers running just prior to termination.

at_exit do
  File.delete("temporary.txt") if File.exist?("temporary.txt")
end

exit 1

In this example, the cleanup block runs even though the script returns failure status. Use exit! only when skipping registered handlers is intentional: it terminates immediately and can leave cleanup undone.

Why a Ruby program returns status 1

An unhandled exception prints exception details to STDERR and sets the process status to 1. If a command unexpectedly fails, check whether an exception was raised without being rescued. The Stack Overflow explanation of exit and abort also distinguishes that case from deliberate termination with exit or abort.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Further reading

For a deeper reference to Ruby’s language and standard library, see the Programming Ruby reference book.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.