From: Robert Klemme Date: 2003-05-20T16:31:29+09:00 Subject: Re: Exceptions in initialize()? "Jack Twilley" 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" # 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 # 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 # nil irb(main):023:0> irb(main):024:0* p defined? f3 "local-variable" nil irb(main):025:0> robert