# Re: How to exit Ruby program properly

**URL:** <https://www.ruby-forum.com/t/re-how-to-exit-ruby-program-properly/50116>\
**Category:** Ruby\
**Created:** [December 13, 2005, 6:03am UTC](https://www.ruby-forum.com/t/re-how-to-exit-ruby-program-properly/50116 "2005-12-13T06:03:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 13, 2005, 6:03am UTC](https://www.ruby-forum.com/t/re-how-to-exit-ruby-program-properly/50116/1 "2005-12-13T06:03:15Z")

</div>

Process.exit is the way to go (or just putting the rest of your logic  
into the else section of that if statement).

There is nothing unclean about the fact that Process.exit raises an  
exception. The fact that it raises an exception is noted to allow other  
methods up the chain to intercept and possibly prevent the shutdown.

It also ensures that all the “ensure” thingies up the chain get  
executed, so that you file output can be flushed and handles closed and  
any other shutdown procedures can come into play. For example:

file = File.new(“outfile”,“w”)  
begin  
f.puts “Monkeys are cool”  
Process.exit  
ensure  
f.close  
puts “File flushed properly”  
end

(Note that you’d usually pass a block to File.open, which does the  
begin/ensure/close/end bit for you)

If you REALLY want to exit NOW, you can use Process.exit!, but generally  
you don’t want to do that. Change the above the use Process.exit!, and  
you’ll notice that “File flushed properly” will not be output - imagine  
if there was a more important ensure clause hanging around.

> I’m using Process.exit but the documentation implies that it  
> end

#####################################################################################  
This email has been scanned by MailMarshal, an email content filter.  
#####################################################################################
