Exceptions in initialize()?

I’m very new to ruby, so please be gentle.

I have created a class and an initialize() method. I would like an
attempt to initialize() to fail if either an argument is not supplied
or the supplied argument does not match a regular exponent. The
solution would appear to be to use an exception, but I cannot tell

  • From the documentation what happens if an exception is thrown from
    within an initialize() method and not caught.

I know I can “just try it”, but I’m interested in knowing what’s
supposed to happen in that case. I’m hoping that what is supposed to
happen is that the new object is equal to nil, but I can’t be sure of
the defined behavior until it’s defined.

Thank you for your patience and assistance.

Jack.


Jack Twilley
jmt at twilley dot org
http colon slash slash www dot twilley dot org slash tilde jmt slash

I’m very new to ruby, so please be gentle.

I have created a class and an initialize() method. I would like an
attempt to initialize() to fail if either an argument is not supplied
or the supplied argument does not match a regular exponent. The
solution would appear to be to use an exception, but I cannot tell

  • From the documentation what happens if an exception is thrown from
    within an initialize() method and not caught.

I know I can “just try it”, but I’m interested in knowing what’s
supposed to happen in that case. I’m hoping that what is supposed to
happen is that the new object is equal to nil, but I can’t be sure of
the defined behavior until it’s defined.

class Foo; def initialize; raise “foo”; end; end
=> nil
a = Foo.new rescue nil
=> nil
a
=> nil

or

def Foo.new(*args)
begin
?> super(*args)
rescue
nil
end
end
=> nil
a = Foo.new
=> nil

···

On Tue, May 20, 2003 at 03:30:58PM +0900, Jack Twilley wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

There are 3 kinds of people: those who can count & those who can’t.
– Unknown source

“Jack Twilley” jmt+usenet@twilley.org schrieb im Newsbeitrag
news:86u1bqi18y.fsf@duchess.twilley.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I’m very new to ruby, so please be gentle.

I have created a class and an initialize() method. I would like an
attempt to initialize() to fail if either an argument is not supplied
or the supplied argument does not match a regular exponent. The
solution would appear to be to use an exception, but I cannot tell

  • From the documentation what happens if an exception is thrown from
    within an initialize() method and not caught.

    Then the process or thread (depending on the circumstances) is
    terminated.

I know I can “just try it”, but I’m interested in knowing what’s
supposed to happen in that case. I’m hoping that what is supposed to
happen is that the new object is equal to nil, but I can’t be sure of
the defined behavior until it’s defined.

Yes, it's nil because the assignment didn't take place.  But typically

this is not too interesting since the code following the assignment will
not be executed anyway:

irb(main):001:0> class Foo
irb(main):002:1> def initialize(a)
irb(main):003:2> raise “should’ve been ‘a’” unless “a” == a
irb(main):004:2> @a=a
irb(main):005:2> end
irb(main):006:1> end
nil
irb(main):007:0>
irb(main):008:0* f1 = Foo.new “a”
#<Foo:0x2aac998 @a=“a”>
irb(main):009:0>
irb(main):010:0* Thread.new do
irb(main):011:1* f2 = Foo.new "b"
irb(main):012:1> puts "never here"
irb(main):013:1> end
#<Thread:0x2a51e08 dead>
irb(main):014:0>
irb(main):015:0* p defined? f3
nil
nil
irb(main):016:0>
irb(main):017:0* begin
irb(main):018:1* f3 = Foo.new "b"
irb(main):019:1> puts "never here"
irb(main):020:1> rescue => e
irb(main):021:1> p e
irb(main):022:1> end
#<RuntimeError: should’ve been ‘a’>
nil
irb(main):023:0>
irb(main):024:0* p defined? f3
"local-variable"
nil
irb(main):025:0>

robert