Intro to Ruby - Exceptions

Ruby

Overview

The following demonstrates exception handling and creation of custom exceptions within the Ruby language. Study the code below and be sure to read the comments for each example to understand what is going on. BTW, while not shown below, you don’t have to use the begin keyword when dealing with exceptions. Something to keep in mind.

Code


# The following raises and rescues an exception.
puts "01 Exception Rescued\n\n"
begin
  raise
rescue
  puts "Hmm, something happened."
end

# The following raises an exception with a message and then rescues it.
# This time we use the global variable '!', which is a convenient reference to the
# exception, to list details about the exception.
puts "\n02 Exception (with a message) Rescued\n\n"
begin
  raise "Ouch!"
rescue
  puts $!.message
  puts $!.inspect
end

# Example of a valid exception where a method on an object does not exist.
puts "\n03 No Method Exception\n\n"
begin
  ruby = "Ruby"
  ruby.this_is_not_a_method
rescue
  puts "Opps, unknown method."
  puts $!.inspect
end

# Example of a clean run with no errors.  Use of the esle and ensure statements are shown as well.
puts "\n04 A Clean Run\n\n"
begin
  puts "Begin."
rescue
  puts "Opps, something happened."
else
  puts "Else."
ensure
  puts "Ensure."
end

# The following demonstrations creating and using a custom exception.
puts "\n05 A Custom Exception\n\n"
class ExampleError < RuntimeError
  attr_reader :message

  def initialize message
    @message = message
  end
end

begin
  raise ExampleError.new("King for a Day, Fool for a Lifetime")
rescue
  puts $!.message
  puts $!.inspect
end

# The following is a simple example of a catch/throw to deal with exceptions.
puts "\n05 Catch and Throw\n\n"
catch :example do
  puts "I made it here..."
  throw :example
  puts "...but not here."
end

Output

01 Exception Rescued

Hmm, something happened.

02 Exception (with a message) Rescued

Ouch!
#<RuntimeError: Ouch!>

03 No Method Exception

Opps, unknown method.
#<NoMethodError: undefined method `this_is_not_a_method' for "Ruby":String>

04 A Clean Run

Begin.
Else.
Ensure.

05 A Custom Exception

King for a Day, Fool for a Lifetime
#<ExampleError: ExampleError>

05 Catch and Throw

I made it here...

Download

The code above is provided as a Ruby script so that you can quickly execute all the examples above if you like. Just download, unzip, and type the following to execute all examples via the command line: ruby 13-exceptions.rb.

What’s Next

Testing

Tags:

Friday, March 6th, 2009 Software

2 Comments to Intro to Ruby - Exceptions

  1. Exceptions with Ruby were one of my Python “moments”. Why did Matz have to go and rename something that had been around for at least a decade at the time? Try-Throw-Catch was standard.

  2. Kevin McFadden on March 6th, 2009
  3. I agree, catch and throw in Ruby is a bit odd and I have, as of yet, to use it in a useful manner. I find the begin…rescue statements easier to think about.

  4. Brooke Kuhlmann on March 7th, 2009

Leave a comment

You must be logged in to post a comment.