Handling Exceptions in Ruby

September 7, 2014

Applications that are well-designed handle exceptions and errors to prevent crashes. Exceptions occur when any unexpected or exceptional situations arise whilst a program is running. To handle exceptions a programmer will add specific code to handle the error or exceptional state.

In Ruby, you can use a ‘rescue’ block to recover from an exception or just plain ignore the exception and continue on with the program flow. It is common practice to handle the most specific exception to the most general. This way you can accurately control whatever exceptional event occurred in your code.

Here is a block of code that is going to raise an exception, you can run this in IRB:

result = 1 / 0

This will result in a ZeroDivisonError, in my case this is what displayed in IRB

ZeroDivisionError: divided by 0
    from (irb):3:in `/'
    from (irb):3
    from /Users/DeonHeyns/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'

So how do I use a ‘rescue’ block to ensure this doesn’t happen? You will want to wrap your code with a ‘begin rescue’ block.

begin
    1 / 0
rescue
    puts 'Hit something here'
end

This would handle any exception that might happen in your code, however as I stated earlier it is best practice to handle exceptions from most specific to most general. Enter the following in IRB:

begin
    1 / 0
rescue StandardError
    puts 'StandardError'
rescue ZeroDivisionError
    puts 'ZeroDivisionError'
end

Executing this code will result in ‘StandardError’ being printed to the screen. This is because ZeroDivisionError inherits from StandardError resulting in the first block of code being hit.

Let’s swap this code around this time and see the difference.

begin
    1 / 0
rescue ZeroDivisionError
    puts 'ZeroDivisionError'
rescue StandardError
    puts 'StandardError'
end

This results in ‘ZeroDivisionError’ showing up on the screen and that is exactly what we need, we can now handle the most specific error and the more general thereafter. Don’t believe this is the case? Let’s do another quick test. Go ahead and enter the following code in your IRB session.

begin
    1 / 0
rescue LoadError
    puts 'LoadError'
rescue ZeroDivisionError
    puts 'ZeroDivisionError'
rescue StandardError
    puts 'StandardError'
end

The result? The result is that ‘ZeroDivisionError’ is printed to the screen, this proves that the most specific rescue block is hit and if not then the most general rescue block will be hit.

Ruby exception handling is very similar to most other programming languages. If you know that a piece of code is going to raise an exception, you can wrap that code in a ‘begin rescue’ block to ensure that your application does not exit and cause your user to rage.


Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.

comments powered by Disqus