On Mar 23, 2010, at 2:45 PM, Robert K. wrote:
def initialize(status_code)
exception class there is an error, if a is a String a StandardError is
raised).
Not exactly so. According to the passage from RI below, “raise” with an
exception class as a first paremeter accepts up to 2 additional
parameters – the first one will be used as an argument to new when the
exception object is instantiated, and the second, if any, must be an
array representing callback information.
When I need an exception class with more than one argument, I use
something like the following:
class DimensionError < RuntimeError
attr_reader :width, :hight
def initialize(*args)
@width, @hight = *args
super “Wrong dimension #{width}x#{hight}”
end
end
begin
raise DimensionError, [ 5, 3 ]
rescue DimensionError => error
error.wigth
error.length
end
= OR =
class DimensionError < RuntimeError
attr_reader :width, :hight
def initialize(options = {})
@width = options[:width]
@hight = options[:hight]
super “Wrong dimension #{width}x#{hight}”
end
end
begin
raise DimensionError, :width => 5, :hight => 3
rescue DimensionError => error
error.wigth
error.length
end
----------------------------------------------------------- Kernel#raise
raise
raise(string)
raise(exception [, string [, array]])
fail
fail(string)
fail(exception [, string [, array]])
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.
raise "Failed to create socket"
raise ArgumentError, "No parameters", caller