From: Robert Klemme Date: 2010-03-24T17:31:53+09:00 Subject: Re: what's the suggested raise Exception idiom? 2010/3/24 Gennady Bystritsky : > > On Mar 23, 2010, at 2:45 PM, Robert Klemme wrote: >> I believe raise a,b,c,d... is merely syntactig sugar for raise >> a.new(b,c,d...) with a bit of added functionality (e.g. if a is not an >> 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. Right you are. Thanks for the education! Apparently I never had the need for this. :-) > 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 That does not seem to work as you expect on 1.9: irb(main):038:0> class E1 < Exception irb(main):039:1> def initialize(*a) p a end irb(main):040:1> end => nil irb(main):041:0> begin; raise E1,[1,2]; rescue Exception => e; p e; end [[1, 2]] # => # irb(main):042:0> class E1 < Exception irb(main):043:1> def initialize(*a) x,y=*a; p x,y end irb(main):044:1> end => nil irb(main):045:0> begin; raise E1,[1,2]; rescue Exception => e; p e; end [1, 2] nil # => # irb(main):046:0> RUBY_VERSION => "1.9.1" You would have to do irb(main):047:0> class E1 < Exception irb(main):048:1> def initialize(a) x,y=*a; p x,y end irb(main):049:1> end => nil irb(main):050:0> begin; raise E1,[1,2]; rescue Exception => e; p e; end 1 2 # => # irb(main):051:0> Note the missing splash operator in initialize's argument list. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/