Intro to Ruby - Exceptions

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
Article Series - Intro to Ruby
- Intro to Ruby - Getting Started
- Intro to Ruby - Strings
- Intro to Ruby - Numbers
- Intro to Ruby - Ranges
- Intro to Ruby - Arrays
- Intro to Ruby - Hashes
- Intro to Ruby - Variables and Scopes
- Intro to Ruby - Methods
- Intro to Ruby - Blocks
- Intro to Ruby - Classes
- Intro to Ruby - Modules
- Intro to Ruby - Conditionals
- Intro to Ruby - Loops
- Intro to Ruby - Exceptions
- Intro to Ruby - Testing
- Intro to Ruby - Next Steps
2 Comments to Intro to Ruby - Exceptions
Leave a comment
You must be logged in to post a comment.
Search
Categories
- Adventures
(112)
- Announcements
(36)
- Business
(19)
- Electronics
(21)
- Epicurean
(10)
- Games
(3)
- Literature
(1)
- Mechanical
(4)
- Meetups
(18)
- Movies
(2)
- Music
(26)
- Photography
(1)
- Services
(27)
- Software
(132)
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.
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.