From: Fuad Saud Date: 2013-09-15T18:17:19-03:00 Subject: [ruby-core:57226] Re: [ruby-trunk - Feature #8912] Exception.raise --047d7b3440c8e08fcb04e6729cae Content-Type: text/plain; charset=ISO-8859-1 Raising doesn't feel like a proper responsability of the exception. On Sep 15, 2013 3:42 AM, "matz (Yukihiro Matsumoto)" wrote: > > Issue #8912 has been updated by matz (Yukihiro Matsumoto). > > > As Nobu pointed out, we need to use the proper terms: > > * Exception.raise instead of Exception#raise > * Exception.new instead of Exception#new > > We have to distinguish class methods and instance methods. > > Then I think we need a way to separate allocating and raising. > By providing Exception.raise (and discouraging Kernel#raise), there's no > sufficient way to raise pre-allocated exceptions. > Kernel raise takes either exception class or exception instance. > > If we really need to emphasize OO way in raising exception as you claim, > I'd rather add Exception#raise, i.e. > > MyException.new(:foo,:bar).raise > > But in reality, I prefer traditional > > raise MyException.new(:foo,:bar) > > because 'raise' is a core operation, that often is implemented by the > reserved keyword in other languages, > and maybe we will replace it by (somewhat-soft) keyword in the future. > > Matz. > > > ---------------------------------------- > Feature #8912: Exception.raise > https://bugs.ruby-lang.org/issues/8912#change-41820 > > Author: sawa (Tsuyoshi Sawada) > Status: Feedback > Priority: Normal > Assignee: > Category: > Target version: > > > =begin > When we have a custom exception class with a custom (({initialize})) > method whose arity is not (({1})): > > class MyException < StandardError > def initialize x, y > super("Something went wrong with #{x.inspect} because #{y}, blah > blah") > end > end > > in order to raise it, we have to create a new instance of it explicitly > using (({new})), and embed that under (({Kernel#raise})). > > raise(MyException.new(:foo, :bar)) > > This is inconvenient, and does not look object oriented. I propose that > there should be (({Exception#raise})), which is public, so that we can do: > > MyException.raise(:foo, :bar) > > A Ruby implementation may be like this: > > class Exception > def self.raise *args; Kernel.send(:raise, *args) end > end > > This will disallow us from calling the private method (({Kernel#raise})) > (without an explicit receiver) within the context of an (({Exception})) > class unless we use (({send})), but I think such use case is rare, and that > should not be a problem. > =end > > > > -- > http://bugs.ruby-lang.org/ > --047d7b3440c8e08fcb04e6729cae Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

Raising doesn't feel like a proper responsability of the exception.<= /p>

On Sep 15, 2013 3:42 AM, "matz (Yukihiro Ma= tsumoto)" <matz@ruby-lang.org= > wrote:

Issue #8912 has been updated by matz (Yukihiro Matsumoto).


As Nobu pointed out, we need to use the proper terms:

=A0* Exception.raise instead of Exception#raise
=A0* Exception.new instead of Exception#new

We have to distinguish class methods and instance methods.

Then I think we need a way to separate allocating and raising.
By providing Exception.raise (and discouraging Kernel#raise), there's n= o sufficient way to raise pre-allocated exceptions.
Kernel raise takes either exception class or exception instance.

If we really need to emphasize OO way in raising exception as you claim, I&= #39;d rather add Exception#raise, i.e.

=A0 MyException.new(:foo,:bar).raise

But in reality, I prefer traditional

=A0 raise MyException.new(:foo,:bar)

because 'raise' is a core operation, that often is implemented by t= he reserved keyword in other languages,
and maybe we will replace it by (somewhat-soft) keyword in the future.

Matz.


----------------------------------------
Feature #8912: Exception.raise
https://bugs.ruby-lang.org/issues/8912#change-41820

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


=3Dbegin
When we have a custom exception class with a custom (({initialize})) method= whose arity is not (({1})):

=A0 =A0 class MyException < StandardError
=A0 =A0 =A0 def initialize x, y
=A0 =A0 =A0 =A0 super("Something went wrong with #{x.inspect} because = #{y}, blah blah")
=A0 =A0 =A0 end
=A0 =A0 end

in order to raise it, we have to create a new instance of it explicitly usi= ng (({new})), and embed that under (({Kernel#raise})).

=A0 =A0 raise(MyException.new(:foo, :bar))

This is inconvenient, and does not look object oriented. I propose that the= re should be (({Exception#raise})), which is public, so that we can do:

=A0 =A0 MyException.raise(:foo, :bar)

A Ruby implementation may be like this:

=A0 =A0 class Exception
=A0 =A0 =A0 def self.raise *args; Kernel.send(:raise, *args) end
=A0 =A0 end

This will disallow us from calling the private method (({Kernel#raise})) (w= ithout an explicit receiver) within the context of an (({Exception})) class= unless we use (({send})), but I think such use case is rare, and that shou= ld not be a problem.
=3Dend



--
http://bugs.ruby-l= ang.org/
--047d7b3440c8e08fcb04e6729cae--