class Die
def initialize(sides)
@sides=sides
unless @sides>0
raise ArgumentError.new("Your number sucks, yo")
# As per the doc you could write the above line as below also
# raise ArgumentError, "Your number sucks, yo"
end
end
#returns the number of sides of a die
def sides
@sides
end
#generates a random die roll based on the number of sides
def roll
rand(@sides)+1
end
end
Die.new(0) rescue $!.message == "Your number sucks, yo"
# => true
#above can also be written
begin
Die.new(0)
rescue ArgumentError => e
bad = e.message
end
bad == "Your number sucks, yo" # => true
####From the documentation of Exception
When an exception has been raised but not yet handled (in rescue
, ensure
, at_exit
and END
blocks) the global variable $!
will contain the current exception and $@
contains the current exception’s backtrace.
So once I have just raised exception in the $!
global variable, I can use Exception#message
method, which returns the exception’s message or name.
####You use Kernel#raise
With no arguments, raises the exception in $!
or raises a RuntimeError
if $!
is nil
. With a single String
argument, raises a RuntimeError
with the string as a message. Otherwise, the first parameter should be the name of an Exception
class (or an object that returns an Exception
object when sent an exception message). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue clause of begin...end
blocks.